AKUL Cprogramming Practical

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

GURU GOBIND SINGH INDRAPRASTHA

UNIVERSITY

COURSE – BCA(1ST YEAR)


SEMESTER 1
‘C’ PROGRAMMING
SUBJECT CODE: BCA-171
LAB FILE

Submitted To: Submitted By:

Ms. Smitha. M Name: AKUL BHASIN


(Assistant Professor) Enrollment Number:
Class & Section: BCA E2

INSTITUTE OF INNOVATION
IN TECHNOLOGY &
MANAGEMENT
INDEX
S. No. Problem Statement Date Signature

Write a program to convert temperature from


1. Celsius to Fahrenheit by taking input from the
user.
Write a program to find the greatest number
2. among 3 numbers given by the user.

Write a program to check if a given number


3. is a prime number or not.
Write a program to check if a given number
4. is odd or even.
Write a program to find the i. area of circle
5. ii. Area of circle iii. Area of rectangle.
Write a program to swap the value of two
6. variable with the help of a third variable.
Write a program to display the following pattern
7. upto N rows, taking the value of N from the user:

8 Write a menu driven program to construct a


calculator for following arithmetic operations:
addition, subtraction, multiplication,division,
average and percentage.

Write a program to calculate factorial of a


9. number and display fibonacci series upto N
terms using recursive functions.
Write a program to input marks of 50
10.
students using an array and display the
average marks of the class.
Write a program to search for a number
11. entered by the user in a given array and
display the array in ascending order.
Write a program to check if a string is
12. palindrome or not.
Write a menu driven program to perform the
13. following operations: (i) Print armstrong
numbers upto N, (ii) Display prime numbers
between 1 to N, (iii) Reverse of an integer.
Write a program to perform (i) matrix addition,
14. (ii) matrix multiplication, and (iii) Matrix
transpose on 2D arrays
Write a menu driven program to implement the
15. following string operations: (i) Calculate length
of a string (ii) Concatenate at the end of a given
(iii) Copy one string to another (iv) Compare
contents of two strings (v) Copy nth character
string to another.

Write a program to create a structure for


16. employees containing the following data
members: Employee ID, Employee Name, Age,
Address, Department and Salary. Input data for
10 employees and display the details of the
employee from the employee ID given by the
user.

Write a program to add, subtract, multiply


17. and divide two numbers using pointers.
Program 1
question: Write a program to convert temperature from Celsius
to Fahrenheit by taking input from the user.
Code:
#include<stdio.h>
void main()
{
float celsius,fahrenheit;
clrscr();

printf("\n Enter the Temparature in Celsius : ");


scanf("%f",&celsius);

fahrenheit = (1.8 * celsius) + 32;


printf("\n Temperature in Fahrenheit : %f ",fahrenheit);

getch();
}

Output:

Program 2
Ques: Write a program to find the greatest number among 3
numbers given by the user.
Code:
#include<stdio.h>
int main ()
{
int num1, num2, num3;
printf ("Enter first number:");
scanf ("%d”, &num1);
printf ("\nEnter second number:");
scanf ("%d”, &num2);
printf ("\nEnter third number:");
scanf ("%d”, &num3);
if(num1>num2 && num1>num3)
{
printf ("\nFirst number is greater than other numbers");
}
else if(num2>num1 && num2>num3)
{
printf ("\nSecond number is greater than other numbers");
}
else
{
printf ("\nThird number is greater than other numbers");
}
Return 0;
}

Output:
Program 3
Ques: Write a program to check if a given number is a prime
number or not.
Code:
#include <stdio.h>

int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

if (n == 0 || n == 1){
flag = 1;
}
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0){
printf("%d is a prime number.", n);
}
else{
printf("%d is not a prime number.", n);
}
return 0;
}
Output:
Program 4
Ques: Write a program to check if a given number is odd or even.
Code:
#include<stdio.h>
int main ()
{
int n;
printf ("Enter a number:");
scanf ("%d”, &n);
if(n%2==0)
{
printf ("It is an even number.");
}
else
{
printf ("It is an odd number.");
}
Return 0;
}

Output:
Program 5
Ques: Write a program to find the area of (i) Circle (ii) Rectangle.
Code:
(i)
#include<stdio.h>
int main ()
{
int radius;
float area;
printf ("Enter radius:");
scanf ("%d”, &radius);
area = 3.14*radius*radius;
printf ("Area of circle is %f”, area);
return 0;
}
Output:

(ii)
#include<stdio.h>
int main()
{
int length, breadth;
float area;
printf ("Enter length:");
scanf ("%d”, &length);
printf ("Enter breadth:");
scanf ("%d”, &breadth);
area = length*breadth;
printf ("Area of rectangle is %f", area);
return 0;
}
Output:
Program 6
Ques: Write a program to swap the value of two variable with the
help of third variable.
Code:
#include<stdio.h>
int main ()
{
int a, b, c;
printf ("Enter the value of a:");
scanf ("%d”, &a);
printf ("Enter the value of b:");
scanf ("%d”, &b);
printf ("\nNumbers before swapping are %d %d “, a, b);
c=a;
a=b;
b=c;
printf ("\nNumbers after swapping are %d %d “, a, b);
return 0;
}

Output:
Program 7
Ques: Write a program to display the following pattern upto n
rows, taking the value of n from the user.
1
23
456
7 8 9 10
Code:
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
clrscr();
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}
Output:
Program 8
Ques: Write a menu driven program to construct a calculator for
following Arithmetic Operation: addition, subtraction,
multiplication, division, average and percentage.
Code:
#include <stdio.h>
int main ()
{
float num1;
float num2;
char op;
float result;

printf ("Enter the first number:");


scanf ("%f”, &num1);

printf ("Enter the operation:");


scanf (" %c”, &op);

printf ("Enter the second number:");


scanf ("%f”, &num2);

switch(op)
{
case '-':
result = num1 - num2;
printf ("%f”, result);
break;

case '+':
result = num1 + num2;
printf ("%f”, result);
break;
case '*':
result = num1 * num2;
printf ("%f”, result);
break;

case '/':
result = num1 / num2;
printf ("%f”, result);
break;

case 'a':
result = (num1 + num2)/2;
printf ("%f”, result);
break;

case 'p':
result = ((num1 + num2)/200) *100;
printf ("%f”, result);
break;

default:
printf ("the operator is not valid:");
}
Return 0;
}
Output:
Program 9
Ques: Write a program to calculate factorial of a number and
display Fibonacci series upto N terms using recursive functions.
Code:
#include<stdio.h>
void printFibonacci (int n) {
static int n1=0, n2=1, n3;
if(n>0) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf ("%d “, n3);
printFibonacci(n-1);
}
}
int main () {
int n;
printf ("Enter the number of elements: ");
scanf ("%d”, &n);
printf ("Fibonacci Series: ");
printf ("%d %d ",0,1);
printFibonacci(n-2) ;//n-2 because 2 numbers are already printed
return 0;
}
Output:

Program 10
Ques: Write a program to input marks of 50 students using an
array and display the average marks of the class.
Code:
#include<stdio.h>
int main ()
{
int marks [50], i, n, sum =0;
double average;

printf ("Enter numbers:");


scanf ("%d”, &n);
for (i=0; i<n; ++i)
{
printf ("Enter number %d:”, i+1);
scanf ("%d”, &marks[i]);

sum += marks[i];
}
average = (double) sum / n;
printf ("Average = %.2lf", average);
return 0;
}
Output:

Program 11
Ques: Write a program to search for a number entered by the
user in a given array and display the array in ascending order.
Code:
#include <stdio.h>
void main (){
int num[20]={3,4,1,5,2,6};
int i, j, a, n , no,flag=0;
printf("enter the number you want to search it");
scanf("%d",&no);

for (i = 0; i < 6; ++i){


for (j = i + 1; j < 6; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:\n");
for (i = 0; i < 6; ++i){
printf("%d", num[i]);
If(no==num[i]){
Flag = 1;
}
}
If(flag == 1){
printf("found it");
}
Else{
printf("we don't found");
}
Output:
Program 12
Ques: Write a program to check if a string is palindrome or not.
Code:
#include <stdio.h>
#include <string.h>

int main () {
char string1[20];
int i, length;
int flag = 0;
printf ("Enter a string: ");
scanf ("%s", string1);
length = strlen(string1);
for (i = 0; i < length / 2; i++) {
if (string1[i]! = string1[length - i - 1]) {
flag = 1;
break;
}
}
if (flag) {
printf ("%s is not a palindrome\n", string1);
} else {
printf ("%s is a palindrome\n", string1);
}

return 0;
}
Output:

Program 13
Ques: write a menu driven program to perform the following
operations: (i) Print armstrong numbers upto N, (ii) Display
prime numbers between 1 to N, (iii) Reverse of an integer.
#include<stdio.h>
void main() {
int choice;
clrscr();
while(1) {
printf("1.print armstrong number : \n");
printf("2. Pirnt Prime number : \n");
printf("3.Print reverse number : \n");
printf("4.Exit\n");
printf("\nYour choice :? ");
scanf("%d",&choice);
switch(choice){
case 1:{
int n,sum,i,t,a;
printf("The Armstrong numbers in between 1 to N are : \n\n\n");
scanf("%d",&n);
for(i = 1;i<=n;i++)
{
t = i;
sum = 0;
while(t != 0){
a = t%10;
sum += a*a*a;
t = t/10;
}
if(sum == i){
printf("%d\n", i);
}
}
break;
}
case 2:{
int j,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);

for(i=1;i<=n;i++){
count = 0;
for(j=2;j<=i/2;j++){
if(i%j==0){
count++;
break;
}
}

if(count==0 && i!= 1){


printf("prime number: %d\n",i);
}
}
break;
}
case 3:{
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d\n", reverse);
break;
}
case 4:{
exit(0);
}
default:
printf("you choose wrong choice");
}
}
Return 0;
}
Output:

Program 14
Ques: Write a program to perform (i)matrix addition, (ii) matrix
multiplication, and (iii) Matrix transpose) on 2D arrays.
Code:
(i) Matrices addtion
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c)

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

for (i = 0; i < r; ++i)


for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

printf("\nSum of two matrices: \n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}
Output

(ii) Matrices multiplication


include<stdio.h>
int main()
{
int a[5][5],b[5][5],m[5][5];
int i,j,r,c,t,k;
printf("\nEnter No of Rows : ");
scanf("%d",&r);
printf("\nEnter No of Columns : ");
scanf("%d",&c);
printf("\nEnter A Matrix : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\nEnter B Matrix : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",b[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
t=0;
for(k=0;k<c;k++)
{
t+=(a[i][k]*b[k][j]);
}
m[i][j]=t;
}
}
printf("\nResult Matrix : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",m[i][j]);
}
printf("\n");
}
return 0;
}

Output:

(iii) Matrices transpose


#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

printf("\nEnter matrix elements:\n");


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
} output:-

Program 15
Write a menu driven program to implement the following string
operations: (i) Calculate length of a string (ii) Concatenate at the
end of a given (iii) Copy one string to another (iv) Compare
contents of two strings (v) Copy nth character string to another.

Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str1[20],str2[20];
int ch,i,j;
do
{
printf("\tMENU");
printf("\n------------------------------\n");
printf("1:Find Length of String");
printf("\n2:Find Reverse of String");
printf("\n3:Concatenate Strings");
printf("\n5:Copy String ");
printf("\n5:Compare Strings");
printf("\n6:Exit");
printf("\n------------------------------\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter String: ");
scanf("%s",str1);
i=strlen(str1);
printf("Length of String : %d\n\n",i);
break;
case 2:
printf("Enter String: ");
scanf("%s",str1);
//strrev(str1);
printf("Reverse string : %s\n\n",str1);
break;
case 3:
printf("\nEnter First String: ");
scanf("%s",str1);
printf("Enter Second string: ");
scanf("%s",str2);
strcat(str1,str2);
printf("String After Concatenation : %s\n\n",str1);
break;
case 4:
printf("Enter a String1: ");
scanf("%s",str1);
printf("Enter a String2: ");
scanf("%s",str2);
printf("\nString Before Copied:\
nString1=\"%s\",String2=\"%s\"\n",str1,str2);
strcpy(str2,str1);
printf("-----------------------------------------------\n");
printf("\"We are copying string String1 to String2\" \n");
printf("-----------------------------------------------\n");
printf("String After Copied:\nString1=\"%s\",
String2=\"%s\"\n\n",str1,str2);
break;
case 5:
printf("Enter First String: ");
scanf("%s",str1);
printf("Enter Second String: ");
scanf("%s",str2);
j=strcmp(str1,str2);
if(j==0)
{
printf("Strings are Same\n\n");
}
else
{
printf("Strings are Not Same\n\n");
}
break;
case 6:
exit(0);
break;
default:
printf("Invalid Input. Please Enter valid Input.\n\n ");
}
}while(ch!=6);
return 0;
}
Output
Output:
Program 16
Write a program to create a structure for employees containing
the following data members: Employee ID, Employee Name, Age,
Address, Department and Salary. Input data for 10 employees
and display the details of the employee from the employee ID
given by the user.

Code:

#include<stdio.h>

struct employee{
int employeeID;
char name[50];
int age;
char address[100];
char department[100];
float salary;
};

int main(){
struct employee emp[10];
int i,id;
clrscr();
for(i=1;i<=10;i++){
printf("input the details for\n %d\nEmployeeid:\
nname:\nAge:\nDepartment:\nsalary:\n",i);
scanf("%d",&emp[i].employeeID);
scanf("%s",emp[i].name);
scanf("%d",&emp[i].age);
scanf("%s",emp[i].address);
scanf("%s",emp[i].department);
scanf("%.2f",&emp[i].salary);
}
printf("enter id of employee you want to search");
scanf("%d",&id);
for(i=1;i<=10;i++){
if(id==emp[i].employeeID){
printf("%d employee");
printf("%d",emp[i].employeeID);
printf("%s",emp[i].name);
printf("%d",emp[i].age);
printf("%s",emp[i].address);
printf("%s",emp[i].department);
printf("%.2f",emp[i].salary);
}
}
return 0;
}
output:
Program 17
Write a program to add, subtract, multiply and divide two
numbers using pointers.

Code:

#include<stdio.h>

void main()
{
int num1,num2,sub,*p1,*p2,add,div,mul;
clrscr();
printf("Enter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
p1=&num1;
p2=&num2;
add=*p1+*p2;
sub=*p1-*p2;
mul=*p1 * *p2;
div=*p1/*p2;
printf("\n\nDifference of %d and %d is %d",*p1,*p2,sub);
printf("\n\nAddition of %d and %d is %d",*p1,*p2,add);
printf("\n\n multiplication of %d and %d is %d",*p1,*p2,mul);
printf("\n\nDivision of %d and %d is %d",*p1,*p2,div);

getch();
}
Output:

You might also like