C Language Lab Manual
C Language Lab Manual
Program
#include<stdio.h>
int main()
char c;
printf("enter a character");
scanf("%c",&c);
if(c>=65&&c<=90)
c=c+32;
printf("%c",c);
else
Output
enter a characterD
enter a character a
2. Finding the Sizes and Ranges of different types.(Hint: Use sizeof() and limits.h)
Program:
#include<stdio.h>
#include<limits.h>
int main()
char c;
int a;
float b;
double d;
printf("%d %d %d %d",sizeof(a),sizeof(c),sizeof(float),sizeof(double));
printf("%d %d",INT_MIN,INT_MAX);
printf("%d %d",CHAR_MIN,CHAR_MAX);
printf(“%f %f”,FLT_MIN,FLT_MAX);
printf(“%lf %lf”,DBL_MIN,DBL_MAX);
output
4 1 4 8-2147483648 2147483647
-2147483648 2147483647
0.000000 340282346638528860000000000000000000000.000000
1797693134862315700000000000000000000000000000000000000000000000000000
Programming with C
Program:
#include<stdio.h>
#include<math.h>
int main()
int a,b,c,d;
double r1,r2;
printf("enter a b c values");
scanf("%d %d %d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d<0)
else if(d==0)
r1=-b/(2*a);
r2=r1;
printf("%d %d",r1,r2);
else
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("%d %d",r1,r2);
}
Programming with C
Output:
enter a b c values1
enter a b c values4
4. Print whether the given number is perfect (for a perfect number, the sum of divisors-except the number
Program:
#include<stdio.h>
int main()
int n,i=1,j,sum=0;
scanf("%d",&n);
while(i<n)
j=n%i;
if(j==0)
sum=sum+i;
i++;
if(sum==n)
else
Output
enter a number6
enter a number121
5. First n terms of Fibonacci Sequence using (i) any loop and (ii) if statement (use 'switch'to decide
the choice).
Program:
#include<stdio.h>
int main()
{
int n,i,x=1,y=0,z=0;
scanf("%d",&n);
for (i=1;i<=n;i++)
{ printf("%d,",z);
z=x+y;
x=y;
y=z;
Output:
0,1,1,2,3,
Programming with C
#include<stdio.h>
int main()
{
int n,i,a,b,c;
printf("Enter a number: ");
scanf("%d",&n);
i=1;
a=0;
b=1;
while(i<=n)
{
printf("%d ",a);
c = a + b;
a = b;
b = c;
i++;
}
return 0;
}
#include<stdio.h>
int main()
{
int a,b,c,n,i;
a=0;
b=1;
i=3;
printf("Enter the number of terms ");
scanf("%d",&n);
printf("%d\n%d",a,b);
do
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
i=i+1;
}
while (i<=n);
return 0;
}
Programming with C
#include <stdio.h>
int fibo(int);
int main()
{
int num;
int result;
Output
program:
#include<stdio.h>
#include<stdlib.h>
main()
int i,j,k,num,add=0;
int arr[10000];
printf(“enter the max values upto which you want to check twin primes”);
scanf(“%d”,&num);
for(i=3;i<num;i++)
k=0;
for(j=2;j<i;j++)
if(i%j==0)
k++;
if(k==0)
arr[add]=i;
add++;
}}
for(i=0;i<add;i++)
if(arr[i]==arr[i-1]+2)
Programming with C
printf(“%d %d\n”,arr[i-1],arr[i]);
Output
enter the max values upto which you want to check twin primes100
35
57
11 13
17 19
29 31
41 43
59 61
71 73
Programming with C
5. Generate one hundred random integers in the range of 1 to 100, store them in an array and print the
#include<stdio.h>
#include<time.h>
main()
int r[100],I,sum=0;
double avg;
srand(time(0));
for(i=0;i<100;i++)
r[i]=rand()%100+i;
printf(“%4d”,r[i]);
sum+=r[i];
avg=(double) sum/100;
printf(“\n avg=%lf”,avg);
#include<stdio.h>
#include<time.h>
main()
int r[100],i=0,sum=0;
double avg;
strand(time(0));
while(i<100)
r[i]=rand()%100+i;
Programming with C
printf(“%4d”,r[i]);
sum+=r[i];
i++;
avg=(double) sum/100;
printf(“\n avg=%lf”,avg);
#include<stdio.h>
#include<time.h>
main()
int r[100],i=0,sum=0;
double avg;
srand(time(0));
do{
r[i]=rand()%100+l;
printf(“%4d”,r[i]);
sum+=r[i]; }
avg=(double) sum/100;
printf(“\n avg=%lf”,avg); }
Output:
21 93 71 46 26 24 59 20 13 50 15 40 51 28 86 51 85 25 98 79
46 54 139 112 129 88 63 71 105 82 146 121 71 109 141 136 135 107 120 67
121 81 157 64 76 134 137 109 155 77 151 106 84 86 164 115 93 176 167 106
137 113 134 163 109 86 101 118 143 137 141 93 130 189 143 187 174 152 146 168
avg=97.200000
Programming with C
5. Print the average of the given numbers and also the numbers greater than the average.
Program:
#include<stdio.h>
main()
int avg,i,n,a[20],sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=sum+a[i];
avg=sum/n;
printf("avg=%d\n",avg);
for(i=0;i<n;i++)
if(a[i]>avg)
printf("%d\n",a[i]);
}
Programming with C
Output:
avg=2
3
Programming with C
6. Generate one hundred random integers in the range of 1 to 100, store them in an array and
program:
#include<stdio.h>
#include<time.h>
main()
int r[100],I,sum=0;
double avg;
strand(time(0));
for(i=0;i<100;i++)
r[i]=rand()%100+i;
printf(“%4d”,r[i]);
sum+=r[i];
svg=(double) sum/100;
printf(“\n avg=%lf”,avg);
#include<stdio.h>
#include<time.h>
main()
int r[100],i=0,sum=0;
double avg;
strand(time(0));
while(i<100)
r[i]=rand()%100+i;
Programming with C
printf(“%4d”,r[i]);
sum+=r[i];
i++;
avg=(double) sum/100;
printf(“\n avg=%lf”,avg);
#include<stdio.h>
#include<time.h>
main()
int r[100],i=0,sum=0;
double avg;
strand(time(0));
do
r[i]=rand()%100+l;
printf(“%4d”,r[i]);
Sum+=r[i];
avg=(double) sum/100;
printf(“\n avg=%lf”,avg);
}
Programming with C
7. Print the average of the given numbers and also the numbers greater than the average.
Program:
#include<stdio.h>
main()
int avg,i,n,a[20],sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
sum=sum+a[i];
avg=sum/n;
printf("avg=%d\n",avg);
for(i=0;i<n;i++)
{ if(a[i]>avg)
printf("%d\n",a[i]);
}}
avg=3
5
Programming with C
Program:
#include<stdio.h>
main()
{
int n,b[20],i=0,q;
printf("enter decimal number");
scanf("%d",&n);
while(n>0)
{
b[i]=n%2;
n=n/2;
i++;
}
for(i=i-1;i>=0;i--)
printf("%d",b[i]);
}
Output:
Enter decimal number 12
1100
Programming with C
Program:
#include<stdio.h>
void read_matrix(int [][5],int,int);
int matrix_mul(int[][5],int,int,int[][5],int,int,int[][5]);
void write_matrix(int[][5],int,int);
main()
{
int A[5][5],B[5][5],C[5][5],r1,c1,r2,c2,i,j,f;
printf("enter size of matrix A");
scanf("%d%d",&r1,&c1);
printf("\nenter elements of matrix A");
read_matrix(A,r1,c1);
printf("enter size of matrix B");
scanf("%d%d",&r2,&c2);
printf("\nenter elements of matrix B");
read_matrix(B,r2,c2);
f=matrix_mul(A,r1,c1,B,r2,c2,C);
printf("\nMATRIX A\n");
write_matrix(A,r1,c1);
printf("\nMATRIX B\n");
write_matrix(B,r2,c2);
if(f==1)
{
printf("\nMATRIX C\n");
write_matrix(C,r1,c2);
}
else
printf("\nMATRIX MULTIPLICATION NOT POSSIBLE");
}
}
return 0;
}
Output:
MATRIX A
1 2 3
4 5 6
MATRIX B
6 5
4 3
2 1
MATRIX C
20 14
56 1
Programming with C
Program:
#include<stdio.h>
int main()
{ int a[10][10],r,c;
printf("\n Enter number of rows and columns of the matrix ");
scanf("%d%d",&r,&c);
readmatrix(a,r,c);
printmatrix(a,r,c);
Transposematrix(a,r, c);
}
Program :
#include<stdio.h>
#include<string.h>
int polindrome(char[]);
int main()
{ char a[20];
int i,j=0,flag=0;
printf("\n enetr a string");
scanf(“%s”,a);
flag=polindrome(a);
if(flag==1)
printf("\n given string is a palindrome");
else
printf("\n given string is not a palindrome");
}
13. Sort the given array of strings in dictionary order (use a function).Dictonary Order
Program :
#include<stdio.h>
#include<string.h>
int main()
char s[10][10],t[10];
int i,j,k,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",s[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(s[i],s[j])>0)
strcpy(t,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],t);
for(i=0;i<n;i++)
printf("\n%s",s[i]);
}
Programming with C
Program:
#include<stdio.h>
int main()
void tower(int,char,char,char);
int n;
char first,second,third;
scanf("%d",&n);
//scanf("%c %c %c",&first,&second,&third);
first='A';
second='B';
third='C';
tower(n,first,third,second);
char temp;
if(n==1)
else
if(n>1)
tower(n-1,f,t,s);
tower(n-1,t,s,f);}}
Programming with C
Output:
15. Program that performs all the five arithmetic operations using Pointers.
Program:
#include<stdio.h>
int main()
int a=5,b=10,*p,*q;
int c;
p=&a;
q=&b;
c=*(p++);
}
Programming with C
16. Print the details of students of a class (the details may be : Roll number, name, department, class,
address, marks in five subjects and average of marks) using nested structures (calculate average).
Program:
#include<stdio.h>
int main()
struct name
char fname[20];
char lname[20];
} n;
struct student
struct name n;
int rollno;
char branch[5];
int m1,m2,m3;
}s[3];
int i;
for(i=0;i<3;i++)
scanf("%s",s[i].n.fname);
scanf("%s",s[i].n.lname);
scanf("%d",&s[i].rollno);
scanf("%d %d %d",&s[i].m1,&s[i].m2,&s[i].m3);
Programming with C
for(i=0;i<3;i++)
//sum=s[i].m1+s[i].m2+s[i].m3;
printf("\nName : %s %s",s[i].n.fname,s[i].n.lname);
printf("\n RollNo:%d",s[i].rollno);
Output:
80
80
student details
Name : sandhya v
RollNo:43
Marks1 :70
Marks2:80
Marks:90
Name : tyuu j
RollNo:80
Marks1 :9
Marks2:0
Marks:80
Programming with C
Name : tyy p
RollNo:44
Marks1 :90
Marks2:80
Marks:80
17. Program that demonstrates the memory allocation done by a structure and a union (declare Structure
Program:
#include<stdio.h>
int main()
{ struct student
{ char name[20];
int rollno;
char branch[20];
};
printf("\n Branch:%s",s.branch);
printf("\n Name:%d",sizeof(s.name));
printf("\n RollNo:%d",sizeof(s.rollno));
printf("\n branch:%d",sizeof(s.branch));
}
Programming with C
#include<stdio.h>
union student
{ char name[10];
int roll;
char branch[10];
}s1,s3,s4;
struct student1
{ char name[10];
int roll;
char branch[10];
}s2;
int main()
scanf("%s",s1.name);
scanf("%d",&s3.roll);
scanf("%s",s4.branch);
printf("\n Name:%s",s1.name);
printf("\n RollNo:%d",s3.roll);
printf("\n branch:%s",s4.branch);
}
Programming with C
19. Program that illustrates the function fprintf() to write into a text file.
Program:
#include<stdio.h>
Void main ()
Exit(1);
fprintf (fp,”%s”,”hai”);
}
Programming with C
Program:
#include<stdio.h>
Void main ()
Char ch;
exit(1);
printf(“%c”,ch);
}
Programming with C
21. Program that accepts the names of two files and copies the first file into the second
Program:
#include<stdio.h>
Void main()
Char ch[100],filename1[20],filename2[20]
exit(1);
While (fgets(ch,100,fp)!=NULL)
fputs(ch,fp1);
fclose(fp);
fclose(fp1);