KAMAL INSTITUTE OF HIGHER EDUCATION AND
ADVANCED TECHNOLOGY
(Recognized by Govt. of NCT of Delhi, Affiliated to GGS Indraprastha University, Delhi &
Approved by Bar Council of India)
Nawada, New Delhi
PRACTICAL FILE OF “C Programming”
BCA 1st
Submitted in Partial Fulfillment of the requirement of BCA (Bachelor of Computer Application)
Batch (2022 – 2025)
Name: Gaurav Singh Joon
Enrollment No.: 00996702022
SUBMITTED TO:
DR. RANJEETA KAUR
1
GAURAV SINGH JOON
Practic Assignments Date Signature
al No.
P1 Write a program to convert temperature from Celsius
to Fahrenheit by taking input from the user.
P2 Write a program to find the greatest number among 3
numbers given by the user.
P3 Write a menu driven program to construct a calculator
for following arithmetic operations: addition,
subtraction, multiplication, division, average and
percentage.
P4 Write a program to check if a given number is a prime
number or not.
P5 Write a program to display the following pattern up to
N rows, taking the value of N from the user:
1
2 3
4 5 6
7 8 9 10
P6 Write a program to input the marks of 50 students
using an array and display the average marks of the
class.
P7 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 an
integer
P8 Write a program to search for a number entered by the user in
a given array and display the array in ascending order.
P9 Write a program to calculate factorial of a number
and display fibonacci series upto N terms using
recursive functions.
P10 Write a program to perform matrix addition, (ii)
matrix multiplication, and (iii) Matrix transpose) on
2D arrays.
P11 Write a program to check if a string is a palindrome or
not.
2
GAURAV SINGH JOON
P12 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
P13
Write a program to add, subtract, multiply and divide
two numbers using pointers.
P14 Write a program to read time in string format and
extract hours, minutes and second also check time
validity
P15 Write a program to make use of arrays with structures
in the following ways:
(i) Use array as a structure data member
(ii) Create array of structure variables
3
GAURAV SINGH JOON
PROGRAM 1
QUESTION 1 -Write a program to convert temperature from Celsius to
Fahrenheit by taking input from the user.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, farenheit;
printf(“Enter the temperature in celsius”);
scanf(“%f” , &celsius);
farenheit=(celsius*9/5)+32;
printf(“%.2f Celsius=%.2f Farenheit” , celsius, farenheit);
getch();
}
OUTPUT:
4
GAURAV SINGH JOON
PROGRAM 2
QUESTION 2- Write a program to find the greatest number among 3
numbers given by the user.
PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
{
printf("%d is the greatest number.", n1);
}
else if (n2 >= n1 && n2 >= n3)
{
printf("%d is the greatest number.", n2);
}
else
printf("%d is the greatest number.", n3);
getch();
}
OUTPUT:
5
GAURAV SINGH JOON
PROGRAM 3
QUESTION 3- Write a menu driven program to construct a
calculator for following arithmetic operations: addition,
subtraction, multiplication, division, average &
percentage.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
intmain()
{
intchoice,a,b;
while (1)
{
printf("\
n*******************************************************")
;
printf("\nENTER NUMBER FROM MENU BELOW");
printf("\
n*******************************************************")
;
printf("\n1. ADD");
printf("\n2. SUBTRACT");
6
GAURAV SINGH JOON
printf("\n3. MULITPLY");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\[Link]");
printf("\
n*******************************************************")
;
scanf("%d",&choice);
switch (choice)
{
case 7: exit(0);
case 1: printf("ENTER TWO NUMBERS");
scanf("%d %d",&a,&b);
printf("SUM=%d",a+b);
break;
case 2: printf("ENTER TWO NUMBERS");
scanf("%d %d",&a,&b);
printf("SUBTRACTION=%d",a-b);
break;
case 3: printf("ENTER TWO NUMBERS");
scanf("%d %d",&a,&b);
printf("MULTIPLICATION=%d",a*b);
break;
case 4: printf("ENTER TWO NUMBERS");
scanf("%d %d",&a,&b);
printf("DIVISON=%d",a/b);
break;
case 5: printf("HOW MANY NUMBERS:");
int n1;
scanf("%d",&n1);
int arr1[100];
int sum1=0;
printf("ENTER NUMBERS:");
7
GAURAV SINGH JOON
for(inti=0;i<n1;i++)
{
scanf("%d",&arr1[i]);
sum1+=arr1[i];
}
printf("AVERAGE:%d",sum1/n1);
break;
case 6: printf("HOW MANY NUMBERS:");
int n;
scanf("%d",&n);
float arr[100];
float sum=0;
printf("ENTER NUMBERS:");
for(inti=0;i<n;i++)
{
scanf("%f",&arr[i]);
sum+=arr[i];
}
float total;
printf("\nEnter total:");
scanf("%f",&total);
float avg;
avg=sum*100/total;
printf("PERCENTAGE:%.2f %%",avg);
break;
default: printf("INVALID NUMBER , ENTER AGAIN");
break;
}
}
return 0;
}
8
GAURAV SINGH JOON
OUTPUT:
9
GAURAV SINGH JOON
10
GAURAV SINGH JOON
PROGRAM 4
QUESTION 4-Write a program to check if a given number is a prime
number or not.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int check(int);
void main()
{
inta,i;
printf(“Enter a number”);
scanf(“%d”,&a);
i=check(a);
if(i==1)
printf(“%d is Prime”,a);
else
printf(“%d is not Prime”,a);
getch();
}
intcheck(int n)
{
inti;
for(i=2;i<n;i++)
{
if(n%2==0)
return 0;
}
return 1;
}
OUTPUT:
11
GAURAV SINGH JOON
PROGRAM 5
12
GAURAV SINGH JOON
QUESTION 5- 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
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int,rowsi,j,n=1;
printf("enter the number of rows:\n");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",n);
n++;
}
printf("\n");
}
getch(); }
OUTPUT:
13
GAURAV SINGH JOON
PROGRAM 6
14
GAURAV SINGH JOON
QUESTION 6 -Write a program to input the marks of 50 students
using an array and display the
Average marks of the class.
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
intstudents[50], n, sum ,i;
sum=0;
printf("Enter the no. of students");
scanf("%d",&n);
for(i=0; i<n;i++)
{
printf("Enter the marks of students[%d]= ", i+1);
scanf("%d",&students[i]);
a=a+students[i];
}
{
printf("Average marks of %d students are=%d\n",n,a/n);
}
getch(); }
15
GAURAV SINGH JOON
OUTPUT:
PROGRAM 7
16
GAURAV SINGH JOON
QUESTION 7-Write a menu-driven program to perform the
following operations:-
(i)Print Armstrong numbers up to n
(ii)display prime numbers between 1 to n
(iii)Reverse an integer
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
intnum,temp,rem,sum,n,i,ch,num1,j,f,k,l,num2,rem1,rev;
clrscr();
do
{
printf("\nMenu \n [Link] number upto n\n [Link] numbers
between 1 to n \n [Link] of integer \n [Link]");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter n ");
scanf("%d",&n);
17
GAURAV SINGH JOON
for(i=0;i<=n;i++)
{
num=i;
temp=num;
sum=0;
while(num>0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(temp==sum)
printf("\nArmstrong Number %d",i);
}break;
case 2:
printf("\nEnter N ");
scanf("%d",&k);
for(l=1;l<=k;l++)
{
num1=l;
f=1;
for(j=2;j<num1;j++)
{
18
GAURAV SINGH JOON
if(num1%j==0)
{
f=0;
break;
}
} if(f==1)
printf("\nPrime Number %d",l); }break;
case 3:
printf("\nEnter Integer Number ");
scanf("%d",&num2); rev=0;
while(num2!=0)
{ rem1=num2%10;
rev=rev*10+rem1;
num2=num2/10;
} printf("\nReverse of Integer Number %d",rev); break;
case 4: break;
default: printf("Invalid choice");
} }while(ch!=4);
getch();
}
OUTPUT:
19
GAURAV SINGH JOON
PROGRAM 8
20
GAURAV SINGH JOON
QUESTION 8-Write a program to search for a number entered by the user in
a given
array and display the array in ascending order.
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main() //practical 08
{
inta[20],n,i,s,j,t,f=0;
clrscr();
printf("Enter value of N(<=20)");
scanf("%d",&n);
printf("\nEnter the numbers");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter number to search ");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(a[i]==s)
{
printf("\nNumber found at position %d",i);
f=1;
}
}
if(f==0)
printf("\n Number not found");
for(i=0;i<n;i++)
{
21
GAURAV SINGH JOON
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j]; a[j]=t; } }}
printf("\n Ascending order ");
for(i=0;i<n; i++)
printf("%d ",a[i]);
getch(); }
OUTPUT:
PROGRAM 9
22
GAURAV SINGH JOON
QUESTION 9-Write a program to calculate factorial of a number
and display
Fibonacci series upto N terms using recursive function.
PROGRAM:
#include<stdio.h>
intfactorial(int n)
{
if (n == 0)
return 1;
return n*factorial(n-1);
}
void fibonacci(int n)
{
int t1 = 0, t2 = 1, nextTerm = 0;
printf("Fibonacci Series: ");
for (inti = 1; i<= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
}
intmain()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d is: %d\n", n, factorial(n));
23
GAURAV SINGH JOON
fibonacci(n);
return 0; }
OUTPUT:
PROGRAM 10
24
GAURAV SINGH JOON
QUESTION 10-Write a program to (i)matrix addition (ii) matrix
multiplication (iii)matrix transpose on 2D arrays.
PROGRAM:(i)
#include <stdio.h>
intmain() {
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]); } // adding two matrices
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j) { sum[i][j] = a[i][j] + b[i][j]; }
// printing the result 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;}
25
GAURAV SINGH JOON
OUTPUT:
PROGRAM(ii)
26
GAURAV SINGH JOON
#include<stdio.h>
#include<conio.h>
void main()
//matrix multiplication
inta[2][3]={{1,2,3},{4,5,6}};
intb[3][2]={{1,2},{3,1},{1,1}};int c[2][2],i,j,k; for(i=0;i<2;i++){
for(j=0;j<2;j++) { c[i][j]=0; for(k=0;k<3;k++)
{ c[i][j] +=a[i][k]*b[k][j]; } } } for(i=0;i<2;i++) { for(j=0;j<2;j++)
{ printf("%d",c[i][j]); }getch(); }
Output:
PROGRAM (iii):
27
GAURAV SINGH JOON
/* C program to transpose the 2D array */
#include<stdio.h>
void main()
{ int a[10][10], b[10][10], m, n, i, j;
printf("\nEnter number of rows & columns of array : ");
scanf("%d %d", &m, &n);
printf("\nEnter elements of 2-D array:\n");
for(i=0; i<m; i++)
{ for(j=0; j<n; j++) { scanf("%d", &a[i][j]); } }
printf("\n\n2-D array before transposing:\n\n");
for(i=0; i<m; i++)
{ for(j=0; j<n; j++) { printf("\t%d", a[i][j]); } printf("\n\
n"); /* Transposing array */
for(i=0; i<m; i++) { for(j=0; j<n; j++) { b[j][i] = a[i][j];
} }
printf("\n\n2-D array after transposing:\n\n");
for(i=0; i<n; i++)
{ for(j=0; j<m; j++){ printf("\t%d", b[i][j]); } printf("\n\n"); }
getch();}
OUTPUT:
28
GAURAV SINGH JOON
29
GAURAV SINGH JOON
PROGRAM 11
QUESTION 11-Write a program to check if a string is a palindrome or
not
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){ char str[10]; inti,j,l; clrscr();
printf("enter a string:");
gets(str); //length
for(i=0,l=0;str[i]!='\0';i++) l++;
for(i=0,j=l-1;str[i]==str[j] &&i<=l/2;i++,j--);
if(i>l/2) printf("%f is palindrome",str); else
printf("%f is not palindrome",str);
getch(); }
OUTPUT:
30
GAURAV SINGH JOON
PROGRAM 12
QUESTION 12-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 string
PROGRAM:
/*. Write a menu driven program to implement the following string
operation.
i) Calculate length of a string.
ii) Concatenate two given string.
iii) Copy one string to another iv) Compare contents of two string.
v) Copy nth character string to another.*/
#include<stdio.h>
#include<string.h>
void main(){
int s1[10],s2[10];
intch,i,n;
printf("Menu\n");
printf("\n i) Calculate length of a string. ");
printf("\n ii) Concatenate two given string.");
printf("\n iii) Copy one string to another");
31
GAURAV SINGH JOON
printf("\n iv) Compare contents of two string.");
printf("\n v) Copy nth character string to another.");
printf("\n Enter your choice:");
scanf("%d",&ch); switch(ch){
case 1:
fflush(stdin);
printf("\n enter string: ");
gets(s1);
printf("\n Length : %d",strlen(s1));
break;
case 2:
fflush(stdin);
printf("\n Enter string 1: ");
gets(s1);
printf("\n Enter string 2: ");
gets(s2); printf("\n concatinate: ");
puts(strcat(s1,s2)); break;
case 3:
fflush(stdin);
printf("\n Enter string 1: ");
gets(s1); strcpy(s2,s1);
printf("\nCopied in string 2: "); puts(s2);
32
GAURAV SINGH JOON
break;
case 4:
fflush(stdin);
printf("\n Enter string 1: ");
gets(s1);
printf("\n Enter string 2: ");
gets(s2);
for(i=0;s1[i]!='\0'&& s2[i]!='\0';i++)
if(s1[i]>s2[i])
{
printf("\n%s is greater than %s",s1,s2);break; } else if(s2[i]>s1[i])
{ printf("\n%s is greater than %s",s2,s1);break;
} if(s1[i]=='\0'&s2[i]=='\0')
printf("\n %s and %s are equal",s1,s2); break;
case 5: fflush(stdin);
printf("\n Enter string 1: "); gets(s1);
printf("\n Enter value of n: "); scanf("%d",&n); strncpy(s2,s1,n);
printf("\n copied in string 2: "); puts(s2); break; default:printf("\
nInvalid choice"); }}
OUTPUT:
33
GAURAV SINGH JOON
34
GAURAV SINGH JOON
35
GAURAV SINGH JOON
PROGRAM 13
QUESTION 13-Write a program to add, subtract, multiply, and divide
two number using pointers.
PROGRAM:
#include<stdio.h>
void main()
{
inta,b,add,sub,multiply,divide;
int *aptr=&a, *bptr=&b;
printf("ENTER NUMBERS:");
scanf("%d %d",aptr,bptr);
add=(*aptr)+(*bptr);
sub=(*aptr)-(*bptr);
multiply=(*aptr)*(*bptr);
divide=(*aptr)/(*bptr);
printf("\nADD: %d",add);
printf("\nSUBTRACT: %d",sub);
printf("\nMULTIPLICATION: %d",multiply);
printf("\nDIVISON: %d",divide);
getch();
}
36
GAURAV SINGH JOON
OUTPUT:
37
GAURAV SINGH JOON
PROGRAM 14
QUESTION 14-Write a program to read time in string format
and extract hours, minutes and seconds to check time
validity.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
intmain()
{
ints[10];
inthr,min,sec;
printf("ENTER TIME(HH:MM:SS format)");
scanf("%s",s);
sscanf(s,"%d:%d:%d",&hr,&min,&sec);
printf("%d %d %d",hr,min,sec);
if(hr<24 && min<60 && sec<60)
{
printf("\n TIME VALID");
}
else{
printf("\n Invalid time");
return 0;
}
}
38
GAURAV SINGH JOON
OUTPUT:
39
GAURAV SINGH JOON
PROGRAM 15
QUESTION-Write a program to make use of arrays with
structures in the following ways:
I) Use array as a structure data member
II) Create array of structure values
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
introllno;
intmarks[5];
};
intmain()
{
inttos,tofind;
printf("Enter number of students");
scanf("%d",&tos);
struct student s[tos];
for(inti=0;i<tos;i++)
{
printf("\n Enter name of student %d",i+1);
scanf("%s",s[i].name);
printf("\n Enter roll number of student %d:",i+1);
scanf("%d",&s[i].rollno);
printf("\n Enter marks of student %d:",i+1);
for(int j=0;j<5;j++)
scanf("%d",&s[i].marks[j]);
40
GAURAV SINGH JOON
}
printf("Enter student roll no to find in record:");
scanf("%d",&tofind);
for(inti=0;i<tos;i++)
{
if(tofind==s[i].rollno)
{
printf("\n Name: %s",s[i].name);
printf("\n Roll Number: %d",s[i].rollno);
for(int j=0;j<5;j++)
printf("\n %d",s[i].marks[j]);
exit(0);
}
}
printf("Data not found");
return 0;
}
41
GAURAV SINGH JOON
OUTPUT:
42
GAURAV SINGH JOON