C PROGRAMMING LAB
1. Write a c program to swap the values of two variables without using
third varible
#include<stdio.h>
main()
int a,b;
clrscr();
printf("enter the value for a and b\n");
scanf("%d%d",&a,&b);
printf("values of a and b before swaping\n a=%d b=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("values of a and b after swaping\n a=%d b=%d",a,b);
getch();
******OUTPUT******
Enter the value for a and b
Values of a and b before swapping
a=5 b=3
Values of a and b after swapping
a=3 b=5
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 1
C PROGRAMMING LAB
2. write a c program to find the largest of three numbers ( if-else )
#include<stdio.h>
main()
int a,b,c,large;
clrscr();
printf("enter the value for a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
large=a;
else if(b>c)
large=b;
else
large=c;
printf("the largest number=%d\n",large);
getch();
******OUTPUT******
enter the value for a,b and c
the largest number=5
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 2
C PROGRAMMING LAB
3. write a c program to calculate the roots of a quadratic equation ( using
switch )
#include<stdio.h>
#include<math.h>
main()
float a,b,c,d,r1,r2;
int n;
clrscr();
printf("Enter the value of a,b and c\n");
scanf("%f%f%f",&a,&b,&c);
printf("a=%f\nb=%f\nc=%f\n",a,b,c);
d=b*b-4*a*c;
if(d==0)
n=1;
else if(d>0)
n=2;
else if(d<0)
n=3;
switch(n)
case 1: r1=r2=-b/(2*a);
printf("the given roots are equal\n");
printf("root1=%f\n root2=%f\n",r1,r2);
break;
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 3
C PROGRAMMING LAB
case 2: r1=-b+sqrt(d)/(2*a);
r2=-b-sqrt(d)/(2*a);
printf("the root are real\n");
printf("root1=%f\n root2=%f\n",r1,r2);
break;
case 3: r1=-b/(2*a);
r2=sqrt((fabs)(d))/(2*a);
printf("the roots are real and imagenary\n");
printf("root1=%f\n root2=%f\n",r1,r2);
break;
getch();
******OUTPUT******
Enter the value of a,b and c
a=1.000000
b=2.000000
c=3.000000
the roots are real and imagenary
root1=-1.000000
root2=1.414214
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 4
C PROGRAMMING LAB
4. write a c program to sum and reverse a given integer ( while loop)
#include<stdio.h>
main()
int num,rem,rev=0,sum=0;
clrscr();
printf("enter the number\n");
scanf("%d",&num);
printf("entered number=%d\n",num);
while(num>0)
rem=num%10;
rev=rev*10+rem;
sum=sum+rem;
num=num/10;
printf(" rev=%d\n sum=%d\n",rev,sum);
getch();
******OUTPUT******
enter the number
1234
entered number=1234
rev=4321
sum=10
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 5
C PROGRAMMING LAB
5. write a c program to detect the armstrong numbers in three digits from
100 to 999.( do- while )
#include<stdio.h>
#include<conio.h>
main()
int num,n,rem;
int cube,sum;
clrscr();
printf("armstrong numbers b/w 100 and 999 are:\n");
for(num=100;num<=999;num++)
sum=0;
n=num;
do
rem=n%10;
cube=rem*rem*rem;
sum=sum+cube;
n=n/10;
while(n>0);
if(num==sum)
printf("%d\n",num);
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 6
C PROGRAMMING LAB
getch();
******OUTPUT******
armstrong numbers b/w 100 and 999 are:
153
370
371
407
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 7
C PROGRAMMING LAB
6. write a c program to check whether the given number is prime or not ( for
loop )
#include<stdio.h>
main()
int n,i,flag=0;
printf(“Enter a positive integer:”);
scanf(“%d”,&n);
for(i=2;i<=n/2;++i)
if(n%i==0)
flag=1;
break;
if(n==1)
printf(“1 is neither prime nor comosite”);
else
if(flag==0)
printf(“%d is a prime number”,n);
else
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 8
C PROGRAMMING LAB
printf(“%d is not a prime number”,n);
return(0);
getch();
******OUTPUT******
Enter a positive integer:
3 is a prime number
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 9
C PROGRAMMING LAB
7. write a c program to find the number of and sum of all integers greater
than 100 and less than 200 that are divisible by 7 ( for loop )
#include<stdio.h>
main()
int i,sum=0;
clrscr();
printf("the numbers that are divisible by 7 are \n");
for(i=100;i<=200;i++)
if(i%7==0)
sum=sum+i;
printf("%d\t\n",i);
printf("\n sum=%d\n",sum);
getch();
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 10
C PROGRAMMING LAB
******OUTPUT******
the numbers that are divisible by 7 are
105
112
119
126
133
140
147
154
161
168
175
182
189
196
sum=2107
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 11
C PROGRAMMING LAB
8.write a c program to calculate factorial of a given number using function
#include<stdio.h>
main()
int n,f;
clrscr();
printf("enter the value for n to factorial\n");
scanf("%d",&n);
f=fact(n);
printf("the factorial of %d is=%d\n",n,f);
getch();
int fact(int n)
int i,f=1;
if(n==0)
f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 12
C PROGRAMMING LAB
******OUTPUT******
enter the value for n to factorial
the factorial of 5 is=120
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 13
C PROGRAMMING LAB
9.write a c program to find GCD of two numbers using function
#include<stdio.h>
main()
int a,b,g;
clrscr();
printf("enter the values for a and b\n");
scanf("%d%d",&a,&b);
g=gcd(a,b);
printf("the gcd %d and %d is=%d",a,b,g);
getch();
int gcd(int x,int y)
int temp,n;
temp=x%y;
if(temp!=0)
n=gcd(y,temp);
return(n);
return(y);
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 14
C PROGRAMMING LAB
******OUTPUT******
enter the values for a and b
the gcd 2 and 4 is=2
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 15
C PROGRAMMING LAB
10.write a c program to search for a given number in an array
#include<stdio.h>
main()
int a[10],n,flag,key,i;
clrscr();
printf("Enter the size of the array \n");
scanf("%d",&n);
printf("Enter array elements \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter elements to be searched\n");
scanf("%d",&key);
for(i=0;i<n;i++)
if(a[i]==key)
flag=1;
if(flag==1)
printf("the given element %d is present in the array\n",key);
else
printf("the given element %d is not present in the array\n",key);
getch();
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 16
C PROGRAMMING LAB
******OUTPUT******
Enter the size of the array
Enter array elements
enter elements to be searched
the given element 3 is present in the array
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 17
C PROGRAMMING LAB
11.write a c program to find the transpose of a given matrix
#include<stdio.h>
main()
int a[10][10],i,j,row,col;
clrscr();
printf("enter the value of row and coloum\n");
scanf("%d%d",&row,&col);
printf("enter the matrix elements\n");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
printf("the given matrix is\n");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
printf("%4d",a[i][j]);
printf("\n");
printf("the transpose of given matrix is\n");
for(i=0;i<col;i++)
for(j=0;j<row;j++)
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 18
C PROGRAMMING LAB
printf("%4d",a[j][i]);
printf("\n");
getch();
******OUTPUT******
enter the value of row and coloum
enter the matrix elements
12
34
the given matrix is
1 2
3 4
the transpose of given matrix is
1 3
2 4
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 19
C PROGRAMMING LAB
12.write a c program to addition of two matrices
#include<stdio.h>
main()
int a[10][10],b[10][10],s[10][10],i,j,r,c;
clrscr();
printf("enter the value for row and column\n");
scanf("%d%d",&r,&c);
printf("enter the A matrix element\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("the A matrix is\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%4d",a[i][j]);
printf("\n");
printf("enter the B matrix element\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 20
C PROGRAMMING LAB
printf("the B matrix is\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%4d",b[i][j]);
printf("\n");
printf("the sum of two matrix\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
s[i][j]=a[i][j]+b[i][j];
printf("%4d",s[i][j]);
printf("\n");
getch();
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 21
C PROGRAMMING LAB
******OUTPUT******
enter the value for row and column
enter the A matrix element
12
34
the A matrix is
1 2
3 4
enter the B matrix element
32
15
the B matrix is
3 2
1 5
the sum of two matrix
4 4
4 9
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 22
C PROGRAMMING LAB
13.write a c program to create a structure with employee details and display
the same
#include<stdio.h>
struct emp
int empno;
char name[25],design[20];
float salary;
}e;
main()
clrscr();
printf("enter the employee details\n");
scanf("%d%s%s%f",&e.empno,e.name,e.design,&e.salary);
printf("the employee details are\n empnumber=%d\n",e.empno);
printf("name=%s\n designation=%s\n salary=%f",e.name,e.design,e.salary);
getch();
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 23
C PROGRAMMING LAB
******OUTPUT******
enter the employee details
101
megha
lecturer
15000
the employee details are
empnumber=101
name=megha
designation=lecturer
salary=15000.000000
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 24
C PROGRAMMING LAB
14.write a c program to process student structure containing roll number
class and age as members. The program must read 5 student record in an
array of structure and display the details of a student who is eldest. Use a
function to find the eldest for which array of structure is an argument.
#include<stdio.h>
#include<string.h>
struct student
char name[20];
int regno;
int sem;
int age;
};
void elder(struct student[]);
main()
struct student s[5];
int i;
clrscr();
printf("enter the student 5 information\n");
for(i=0;i<5;i++)
scanf("%s%d%d%d",s[i].name,&s[i].regno,&s[i].sem,&s[i].age);
printf("the student information\n");
printf("name\t\tregno\t\tsem\t\tage\n");
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 25
C PROGRAMMING LAB
for(i=0;i<5;i++)
printf("%s\t\t%d\t\t%d\t\t%d\n",s[i].name,s[i].regno,s[i].sem,s[i].age);
elder(s);
getch();
void elder(struct student s[])
int i,eld,j=0;
eld=s[0].age;
for(i=1;i<5;i++)
if(s[i].age>eld)
eld=s[i].age;
j=i;
printf("the elder student details is:\n");
printf("name=%s\nregno=%d\nsem=%d\nage=%d\n",s[j].name,s[j].regno,s[j].sem,s[j].age);
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 26
C PROGRAMMING LAB
******OUTPUT******
enter the student 5 information
aa
20
bb
21
cc
22
dd
23
ee
25
the student information
name regno sem age
aa 1 2 20
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 27
C PROGRAMMING LAB
bb 2 2 21
cc 3 3 22
dd 4 4 23
ee 5 3 25
the elder student details is:
name=ee
regno=5
sem=3
age=25
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 28
C PROGRAMMING LAB
15.write a c program to demonstrate #define function
#include<stdio.h>
#define PI 3.14157
main()
int r,area;
clrscr();
printf("enter the radious\n");
scanf("%d",&r);
area=PI*(r*r);
printf("area of circle is =%d\n",area);
getch();
******OUTPUT******
enter the radious
area of circle is =28
Department of Computer Science & Engg.,B.E.T.Polytechnic Page 29