0% found this document useful (0 votes)
130 views

C Language Lab Manual

1. The document provides 21 problems to develop C programs for including: converting case of characters, finding data types sizes and ranges, solving quadratic equations, checking for perfect numbers, Fibonacci sequences, twin primes, random number generation, averaging arrays, and more. 2. Some examples of programs provided are ones to convert uppercase to lowercase, find data type sizes and ranges using sizeof and limits.h, solve quadratic equations using if/else statements, check if a number is perfect by comparing the sum of divisors to the number, and print twin prime pairs up to a given limit. 3. The problems cover a wide range of core C programming concepts like data types, conditional statements, loops, functions, file
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

C Language Lab Manual

1. The document provides 21 problems to develop C programs for including: converting case of characters, finding data types sizes and ranges, solving quadratic equations, checking for perfect numbers, Fibonacci sequences, twin primes, random number generation, averaging arrays, and more. 2. Some examples of programs provided are ones to convert uppercase to lowercase, find data type sizes and ranges using sizeof and limits.h, solve quadratic equations using if/else statements, check if a number is perfect by comparing the sum of divisors to the number, and print twin prime pairs up to a given limit. 3. The problems cover a wide range of core C programming concepts like data types, conditional statements, loops, functions, file
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Programming with C

EID121 : PROGRAMMING WITH C LABORATORY LAB CYCLE

Develop C Programs for the following problems:

1. Conversion of an upper-case character to a lower-case character.


2. Finding the Sizes and Ranges of different types.(Hint: Use sizeof() and limits.h)
3. Roots of a Quadratic Equation using 'if'.
4. Print whether the given number is perfect (for a perfect number, the sum of divisors-except the
number itself-will be equal to that number; Exs: 6,28,496, etc.).
5. First n terms of Fibonacci Sequence using (i) any loop and (ii) if statement (use 'switch'to decide
the choice).
6. Print Twin Primes up to a Specified limit.(Exs: 3-5,5-7,11-13, 17-19, etc.)
7. Generate one hundred random integers in the range of 1 to 100, store them in an array and print
the average. (using any loop)
8. Print the average of the given numbers and also the numbers greater than the average.
9. Converting a decimal value to binary.
10. Program that uses a function to perform multiplication of two matrices.
11. Program that uses a function to perform transpose of a given Matrix.
12. Determine if the given string is a Palindrome or not (use a function)
13. Sort the given array of strings in dictionary order (use a function).
14. Recursive and non-recursive functions for Towers of Hanoi.
15. Program that performs all the five arithmetic operations using Pointers.
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).
17. Program that demonstrates the memory allocation done by a structure and a union (declare
Structure and Union in the same program).
18. Program to demonstrate member access in a union (declare three different types of variables in
union, assign values and print them).
19. Program that illustrates the function fprintf() to write into a text file.
20. Program that illustrates the function fscanf() to read from a text file.
21. Program that accepts the names of two files and copies the first file into the second line by line
using fgets() and fputs() functions.Develop C Programs for the following problems:
Programming with C

1. Conversion of an upper-case character to a lower-case character.

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

printf("entered character is not uppercase");

Output

enter a characterD

enter a character a

entered character is not uppercase


Programming with C

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(“%d %d” ,SHRT_MIN,SHRT_MAX);

printf(“%d %d” ,LONG_MIN,LONG_MAX);

printf(“%f %f”,FLT_MIN,FLT_MAX);

printf(“%lf %lf”,DBL_MIN,DBL_MAX);

output

4 1 4 8-2147483648 2147483647

-128 127 -32768 32767

-2147483648 2147483647

0.000000 340282346638528860000000000000000000000.000000

1797693134862315700000000000000000000000000000000000000000000000000000
Programming with C

3. Roots of a Quadratic Equation using 'if'.

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)

printf("roots are imaginary");

else if(d==0)

printf("roots are equal");

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

roots are imaginary

enter a b c values4

roots are equal0 0


Programming with C

4. Print whether the given number is perfect (for a perfect number, the sum of divisors-except the number

itself-will be equal to thatnumber; Exs: 6,28,496, etc.).

Program:

#include<stdio.h>

int main()

int n,i=1,j,sum=0;

printf("\n enter a number");

scanf("%d",&n);

while(i<n)

j=n%i;

if(j==0)

sum=sum+i;

i++;

if(sum==n)

printf("the enetered number %d is a perfect number",n);

else

printf("the enetered number %d is not a perfect number",n);

Output

enter a number6

the enetered number 6 is a perfect number

enter a number121

the enetered number 121 is not a perfect number


Programming with C

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;

printf("Enter the limit\n");

scanf("%d",&n);

for (i=1;i<=n;i++)

{ printf("%d,",z);

z=x+y;

x=y;

y=z;

Output:

Enter the limit

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

b. nth fibnocci series

#include <stdio.h>
int fibo(int);

int main()
{
int num;
int result;

printf("Enter the nth number in fibonacci series: ");


scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}

Output

Enter the nth number in fibonacci series: 5

The 5 number in fibonacci series is 5


Programming with C

6. Print Twin Primes up to a Specified limit.(Exs: 3-5,5-7,11-13, 17-19, etc.)

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++;

}}

printf(“twin primes are\n”);

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

twin primes are

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

average. (using any loop)

Using for loop

#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);

Using while loop

#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);

Using do-while loop

#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

106 42 110 26 32 75 40 54 43 118 105 80 52 117 112 102 116 101 57 86

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;

printf("enter the no.of integers\n");

scanf("%d",&n);

printf("enter the integers:\n");

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

scanf("%d",&a[i]);

sum=sum+a[i];

avg=sum/n;

printf("avg=%d\n",avg);

printf("the no.s greater than average are:\n");

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

if(a[i]>avg)

printf("%d\n",a[i]);

}
Programming with C

Output:

enter the no.of integers

enter the integers:

avg=2

the no.s greater than average are:

3
Programming with C

6. Generate one hundred random integers in the range of 1 to 100, store them in an array and

print the average. (using any loop)

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);

Using while loop:-

#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);

Using do-while loop:-

#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;

printf("enter the no.of integers\n");

scanf("%d",&n);

printf("enter the integers:\n");

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

{ scanf("%d",&a[i]);

sum=sum+a[i];

avg=sum/n;

printf("avg=%d\n",avg);

printf("the no.s greater than average are:");

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

{ if(a[i]>avg)

printf("%d\n",a[i]);

}}

enter the integers:

avg=3

the no.s greater than average are:4

5
Programming with C

9. Program to convert decimal number to binary

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

10. Program that uses a function to perform multiplication of two matrices.

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");
}

void read_matrix(int A[][5],int r,int c)


{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&A[i][j]);
}

int matrix_mul(int A[][5],int r1,int c1,int B[][5],int r2,int


c2,int C[][5])
{
int i,j,k;
if(c1==r2)
{
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
C[i][j]=0;
for(k=0;k<c1;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
return 1;
Programming with C

}
return 0;
}

void write_matrix(int A[][5],int r,int c)


{
int i,j;
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%4d",A[i][j]);
printf("\n");
}
}

Output:

Enter size of matrix A 2 3


Enter elements of matrix A
123456

Enter size of matrix B 3 2


Enter elements of matrix B
654321

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

11. Program that uses a function to perform transpose of a given Matrix.

Program:

#include<stdio.h>

int readmatrix(int a[][10], int r, int c);


int printmatrix(int m[][10], int r, int c);
int Transposematrix(int a[][10], int r, int c);

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);
}

int readmatrix(int a[][10], int r, int c)


{ int i,j;
printf("\n enter the elements of matrix a");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}

int printmatrix(int m[][10], int r, int c)


{ int i,j;
printf("MATRIX elements are\n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
printf("%d\t",m[i][j]);
printf("\n");
}
}

int Transposematrix(int a[][10], int r, int c)


{ int b[10][10], i,j;
printf("\ntranspose of a given matrix is \n");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{ b[j][i]=a[i][j];
}
}
printmatrix(b, c ,r);
}
Programming with C

12. Determine if the given string is a Palindrome or not. (use a function)

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");
}

int polindrome(char a[])


{ int len,i,j;
len=strlen(a);
for(i=0,j=len-1;i<j;i++,j--)
{ if(a[i]!=a[j])
return 0;
}
return 1;
}
Programming with C

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;

printf("\n enter the number of cities");

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

14. Recursive and non-recursive functions for Towers of Hanoi.

Program:

#include<stdio.h>

int main()

void tower(int,char,char,char);

int n;

char first,second,third;

printf("\n enter number of discs");

scanf("%d",&n);

printf("\n enter the towr names");

//scanf("%c %c %c",&first,&second,&third);

first='A';

second='B';

third='C';

tower(n,first,third,second);

void tower(int n,char f,char s, char t)

char temp;

if(n==1)

printf("\n move disk 1 from tower %c to tower %c",f,s);

else

if(n>1)

tower(n-1,f,t,s);

printf("\n move disk %d from tower %c to tower %c",n,f,s);

tower(n-1,t,s,f);}}
Programming with C

Output:

enter number of discs3

enter the towr names

move disk 1 from tower A to tower C

move disk 2 from tower A to tower B

move disk 1 from tower C to tower B

move disk 3 from tower A to tower C

move disk 1 from tower B to tower A

move disk 2 from tower B to tower C

move disk 1 from tower A to tower C


Programming with C

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;

printf("\n Addition a+b =%d",*p+*q);

printf("\n sunstraction a-b=%d",*p-*q);

printf("\n multiply a*b=%d",*p*5);

printf("\n division a/b=%d",*p/2);

c=*(p++);

printf("\n incrementbof pointer %d",*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++)

printf("\n enter student fname");

scanf("%s",s[i].n.fname);

printf("\n enter student lname");

scanf("%s",s[i].n.lname);

printf("\n enter student roll no");

scanf("%d",&s[i].rollno);

printf("\n enter student marks in m1 ,m2 and m3");

scanf("%d %d %d",&s[i].m1,&s[i].m2,&s[i].m3);
Programming with C

printf("\n student details");

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);

printf("\n Marks1 :%d \n Marks2:%d \n Marks:%d",s[i].m1,s[i].m2,s[i].m3);

printf("\n average marks of student %d is %d",s[i].rollno,(s[i].m1+s[i].m2+s[i].m3)/3);

Output:

Enter student roll no44

Enter student marks in m1 ,m2 and m390

80

80

student details

Name : sandhya v

RollNo:43

Marks1 :70

Marks2:80

Marks:90

Average marks of student 43 is 80

Name : tyuu j

RollNo:80

Marks1 :9

Marks2:0

Marks:80
Programming with C

average marks of student 80 is 29

Name : tyy p

RollNo:44

Marks1 :90

Marks2:80

Marks:80

average marks of student 44 is 83


Programming with C

17. Program that demonstrates the memory allocation done by a structure and a union (declare Structure

and Union in the same program).

Program:

#include<stdio.h>

int main()

{ struct student

{ char name[20];

int rollno;

char branch[20];

};

struct student s={"xxx",123,"cs4"};

printf("\n Name : %s",s.name);

printf("\n Roll No:%d",s.rollno);

printf("\n Branch:%s",s.branch);

printf("\n size of structure elements");

printf("\n Name:%d",sizeof(s.name));

printf("\n RollNo:%d",sizeof(s.rollno));

printf("\n branch:%d",sizeof(s.branch));

printf("\n total size:%d",sizeof(s));

}
Programming with C

Using Structures and Unions

#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()

{ printf("\n enter name value");

scanf("%s",s1.name);

printf("\n enter roll no");

scanf("%d",&s3.roll);

printf("enter branch ");

scanf("%s",s4.branch);

printf("\n size of union and structure");

printf("\n union:%d structure :%d",sizeof(s1),sizeof(s2));

printf("\n union member access");

printf("\n student details");

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 ()

FILE *fp; // creating a file pointer

fp = fopen(“filename”,” w”); //open a file in write mode

if(fp==NULL) // to check whether the file is opened properly or not

printf(“ file can’t be opened”);

Exit(1);

fprintf (fp,”%s”,”hai”);

fclose (fp); // close the file

}
Programming with C

20. Program that illustrates the function fscanf () to read.

Program:

#include<stdio.h>

Void main ()

FILE *fp; // creating a file pointer

Char ch;

fp = fopen(“filename”,” r”); //open a file in read mode

if(fp==NULL) // to check whether the file is opened properly or not

printf(“ file can’t be opened”);

exit(1);

While (fscanf (fp,”%c”,ch)!= EOF)

printf(“%c”,ch);

fclose (fp); //close the file

}
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()

{ FILE *fp,*fp1; // creating two file pointers

Char ch[100],filename1[20],filename2[20]

scanf(“%s %s”,filename1,filename2);// reading names of fiels from key board

fp=fopen(“filename1”,”r”); // opening a file in read mode

if(fp==NULL) // to check whether the file is opened properly or not

printf(“ file can’t be opened”); exit(1);

fp1=fopen(“filename2”,”w”);// opening a file in write mode

if(fp1==NULL)// to check whether the file is opened properly or not

printf(“ file can not be opened”);

exit(1);

While (fgets(ch,100,fp)!=NULL)

fputs(ch,fp1);

fclose(fp);

fclose(fp1);

You might also like