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

Lab_DOC_Corrected

Uploaded by

asowadnoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab_DOC_Corrected

Uploaded by

asowadnoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1. Program to find sum of two numbers.

#include<stdio.h> Sample Output:


int main() Enter two no: 5 6
{ sum=11
int a,b,s;
printf(“Enter two no: ”);
scanf(“%d%d",&a,&b);
s=a+b;
printf(“sum=%d”,s);
return 0;
}

2. Program to find area and circumference of circle.


#include<stdio.h>
int main() Output:
{ enter radius of a circle: 5
int r; area of circle=78.000
float pi=3.14,area,ci; circumference=31.4
printf(“enter radius of circle: ”);
scanf(“%d”,&r); area=pi*r*r;
printf(“area of circle=%f ”,area);
ci=2*pi*r;
printf(“circumference=%f ”,ci);
return 0;
}

3. Program to convert temperature from degree centigrade to Fahrenheit.


#include<stdio.h>
int main()
{ Output:
float c,f; enter temp in centigrade: 32 temp
printf(“enter temp in centigrade: ”); in Fahrenheit=89.59998
scanf(“%f”,&c);
f=(1.8*c)+32;
printf(“temp in Fahrenheit=%f ”,f);
return 0;
}

4. Program to calculate sum of 5 subjects and find percentage.


#include<stdio.h>
int main() Output:
{ enter marks of 5 subjects: 60
int s1,s2,s3,s4,s5,sum,total=500; 65
float per; 50
printf(“enter marks of 5 subjects: ”); 60
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5); 60
sum=s1+s2+s3+s4+s5; sum=300
printf(“sum=%d”,sum); percentage=60.000
per=(sum*100)/total;
printf(“percentage=%f”,per);
return 0;
}

5. Program to show swap of two no’s without using third variable.


#include<stdio.h>
int main() Output:
{ enter value for a & b: 4 5
int a,b; after swapping the value of a & b:
printf(“enter value for a & b: ”); 54
scanf(“%d%d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“after swapping the value of a & b: %d
%d”,a,b);
getch();
}

6. Program to reverse a given number.


#include<stdio.h>
int main() Output:
{ enter any no to get its reverse: 456
int n,a,r=0; reverse=654
printf(“enter any no to get its reverse: ”);
scanf(“%d”,&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
}
printf(“reverse=%d”,r);
return 0;
}

7. Program to find greatest in 3 numbers.


#include<stdio.h>
int main() Output:
{ enter value for a, b& c: 5
int a,b,c; 7
printf(“enter value of a, b & c: ”); 4
scanf(“%d%d%d”,&a,&b,&c); b is greatest
if((a>b)&&(a>c))
printf(“a is greatest”);
if((b>c)&&(b>a))
printf(“b is greatest”);
if((c>a)&&(c>b))
printf(“c is greatest”);
return 0;
}
8. Program to show the use of conditional operator.
#include<stdio.h>
int main() Output:
{ enter value for a & b: 5 7
int a,b; b is greater
printf(“enter value for a & b: ”);
scanf(“%d%d”,&a,&b);
(a>b)?printf(“a is greater”):printf(“b is greater”);
return 0;
}

9. Program to find whether given no. is even or odd.


#include<stdio.h>
int main() Output:
{ enter any no: 5
int n; clrscr(); no is odd
printf(“enter any no: ”);
scanf(“%d”,&n);
if(n%2==0)
printf(“no is even”);
else
printf(“no is odd”);
return 0;
}

10. Program to use switch statement. Display Monday to Sunday.


#include<stdio.h>
int main() Output:
{ enter m for Monday
char ch; t for Tuesday
printf(“enter m for Monday\n t for Tuesday\n w w for Wednesday
for Wednesday\n h for Thursday\n f for Friday\n s h for Thursday
for Saturday\n u for Sunday\n”); f for Friday
printf(“enter your choice: “); s for Saturday
u for Sunday
scanf(“%c”,&ch); switch(ch) enter your choice: f
{ friday
case ‘m’:
case ‘M’:
printf(“monday”);
break;
case ‘t’:
case ‘T’:
printf(“tuesday”);
break;
case ‘w’:
case ‘W’:
printf(“wednesday”);
break;
case ‘h’:
case ‘H’:
printf(“thursday”);
break;
case ‘f ’:
case ‘F’:
printf(“friday”);
break;
case ‘s’:
case ‘S’:
printf(“saturday”);
break;
case ‘u’:
case ‘U’:
printf(“sunday”);
break;
default :
printf(“wrong input”);
break;
}
return 0;
}

11. Program to display arithmetic operator using switch case.


#include<stdio.h>
int main() Output:
{ enter two no’s: 8 4
int a,b,n,s,m,su,d; enter 1 for sum
printf(“enter two no’s : ”); 2 for multiply
scanf(“%d%d”,&a,&b); 3 for subtraction
printf(“enter 1 for sum\n2 for multiply\n3for 4 for division: 1
subtraction\n4 for division: ”); sum=12
scanf(“%d”,&n);
switch(n)
{
case 1:
s=a+b;
printf(“sum=%d”,s);
break;
case 2:
m=a*b;
printf(“multiply=%d”,m);
break;
case 3:
su=a-b;
printf(“subtraction=%d”,su);
break;
case 4:
d=a/b;
printf(“divission=%d”,d);
break;
default:
printf(“wrong input”);
break;
}
return 0;
}

12. Program to display first 10 natural no. & their sum.


#include<stdio.h> Output:
int main() 1 no is=1
{ 2 no is=2
int i,sum=0; 3 no is=3
for(i=1;i<=10;i++) 4 no is=4
{ 5 no is=5
printf(“%d no is= %d\n”,i,I); 6 no is=6
sum=sum+i; 7 no is=7
} 8 no is=8
printf(“sum =%d”,sum); 9 no is=9
return 0; 10 no is=10
} sum=55

13. Program to print stars Sequence1:


#include<stdio.h> Output:
int main() *
{ **
int i,j; ***
for(i=1;i<=5;i++) ****
{ *****
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n”);
}
return 0;
}

14. Program to print stars Sequence2:


#include<stdio.h>
int main() Output:
{ *
int i,j; **
for(i=1;i<=5;i++) ***
{ ****
for(j=5;j>=i;j--) *****
printf(“ ”);
for(k=1;k<=i;k++)
printf(“*”);

printf(“\n”);
}
return 0;
}

15. Program to print stars Sequence3.


#include<stdio.h>
int main()
{ Output:
int i,j,k; *
for(i=1;i<=3;i++) ***
{ *****
for(j=3;j>=i;j--)
printf(“ ”);
{
for(k=1;k<=i*2-1;k++)
printf(“*”);
}
printf(“\n”);
}
return 0;
}

16. Program to print Fibonacci series up to 100.


#include<stdio.h>
int main()
{ Output:
int a=1,b=1,c=0,i; 1 1 2 3 5 8 13 21 34 55 89
printf("%d\t%d\t",a,b);
for(i=0;i<=10;i++)
{
c=a+b;
if(c<100)
{
printf("%d\t",c);
}
a=b;
b=c;
}
return 0;
}

17. Program to find factorial of a number.


#include<stdio.h>
int main()
{ Output:
int n,i,fact=1; Enter a no: 5 Factorial=120
printf(“Enter any no: ”);
scanf(“%d”,&n);
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf(“Factorial=%d”,fact);
return 0;
}

18. Program to find whether given no. is a prime no. or not.


#include<stdio.h>
int main()
{ Output:
int i,n,r=0; Enter any no: 16
printf(“Enter any no: ”); Not prime
scanf(“%d”,&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
r=1;
break;
}
if(r==0)
printf(“prime no”);
else
printf(“Not prime”);
return 0;
}

19. Program to add two number using pointers.


#include<stdio.h>
int main() Output:
{ enter two no’s: 10 20
int *p1,*p2,sum; sum=30
printf(“enter two no’s: ”);
scanf(“%d%d”,&*p1,&*p2);
sum=*p1+*p2;
printf(“sum=%d”,sum);
return 0;
}

20. Program to show sum of 10 elements of array & show the average.
#include<stdio.h> Output:
int main() enter elements of an array:
{ 4
int a[10],i,sum=0; 5
float av; 6
printf(“enter elements of an aaray: ”); 1
for(i=0;i<10;i++) 2
scanf(“%d”,&a[i]); 3
for(i=0;i<10;i++) 5
sum=sum+a[i]; 5
printf(“sum=%d”,sum); 4
av=sum/10; 7
printf(“average=%.2f”,av); sum=42
return 0; average=4.22
}

21. Program to find the maximum no. in an array.


#include<stdio.h>
int main() Output:
{ enter elements for array:
int a[5],max,i; 5
printf(“enter element for the array: ”); 4
for(i=0;i<5;i++) 7
scanf(“%d”,&a[i]); 1
max=a[0]; 2
for(i=1;i<5;i++) maximum no= 7
{
if(max<a[i])
max=a[i];
}
printf(“maximum no= %d”,max);
return 0;
}

22. Program to display a matrix.


#include<stdio.h>
int main() enter value for a matrix: 7
{ 8
int a[3][2],b[3][2],i,j; 9
printf(“enter value for a matrix: ”); 4
for(i=0;i<3;i++) 5
{ 6
for(j=0;j<2;j++) enter value for b matrix: 3
scanf(“%d”, &a[i][j]); 2
} 1
printf(“enter value for b matrix: ”); 4
for(i=0;i<3;i++) 5
{ 6
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]); a matrix is
} 7 8
printf(“\na matrix is\n\n”); 9 4
for(i=0;i<3;i++) 5 6
{
for(j=0;j<2;j++) b matrix is
{ 3 2
printf(“ %d ”,a[i][j]); 1 4
} 5 6
printf(“\n”);
}
printf(“\nb matrix is\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d ”, b[i][j]);
}
printf(“\n”);
}
return 0;
}

23. Program to find sum of two matrices.


#include<stdio.h>
int main()
{
int a[3][2],b[3][2],c[3][2],i,j;
printf(“enter value for 1 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”, &a[i][j]);
}
printf(“enter value for 2 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j] = a[i][j] + b[i][j];
}
printf(“\Sum of matrix is matrix is\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d\t”, c[i][j]);
}
printf(“\n”);
}
return 0;
}

23. Program to find subtraction of two matrices. (Similarly)

24. Program to find multiplication of two matrices.


#include<stdio.h>
int main()
{
int a[3][2],b[3][2],c[3][2],i,j;
printf(“enter value for 1 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”, &a[i][j]);
}
printf(“enter value for 2 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j] = a[i][j] *b[i][j];
}
printf(“\Multiplication of matrix 1 and matrix 2 is\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d\t”, c[i][j]);
}
printf(“\n”);
}
return 0;
}

25. Program to find transpose of a matrix.

26. Program to show input and output of a string.


#include<stdio.h>
int main() Output:
{ enter any string: Comilla University
char a[50]; Comilla University
printf(“enter any string: ”);
gets(a);
puts(a);
return 0;
}

27. Program to swap two numbers using functions.


#include<stdio.h>
int main()
{ Output:
int a,b,r; enter value for a & b: 4 5
void swap(int,int); after swapping the value for a & b : 5 4
printf(“enter value for a&b: ”);
scanf(“%d%d”,&a,&b);
swap(a,b);
return 0;
}
void swap (int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf(“after swapping the value for a & b is :
%d %d”,a,b);
}

28. Program to find factorial of a number using functions.


#include<stdio.h>
int main()
{ Output:
int a,f; enter a no: 5
int fact(int); factorial=120
printf(“enter a no: ”);
scanf(“%d”,&a);
f=fact(a);
printf(“factorial= %d”,f);
return 0;
}
int fact(int x)
{
int fac=1,i;
for(i=x;i>=1;i--)
fac=fac*i;
return(fac);
}

29. Program to show call by value.


#include<stdio.h>
int main()
{ Output:
int a,b,swap(); value of a=5 & value of b=10 before swap
a=5; b=10; value of a=5 & b=10 after swap
printf(”value of a=%d & value of b=%d
before swap ”,a,b);
swap(a,b);
printf(“\nvalue of a =%d & b=%d after
swap”,a,b);
return 0;
}
int swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}

30. Program to show call by reference.


#include<stdio.h>
int main() Output:
{ value of a= 5 & value of b=10 before swap
int a,b,*aa,*bb; value of a=10 & b=5 after swap
a=5;
b=10;
aa=&a;
bb=&b;
swap();
printf(“value of a= %d & value of b=%d
before swap”,a,b);
swap(aa,bb);
printf(“\nvalue of a=%d & b=%d after
swap”,a,b);
return 0;
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

31. Program to find factorial of a number using recursion.


#include<stdio.h>
int main() Output:
{ enter number: 5
int n; 5!=120
printf(“enter number: ”);
scanf(“%d”,&n);
if(n<0)
printf(“invalid number”);
else
printf(“%d!=%d”,n,fact(n));
return 0;
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}

32. Program to find whether a string is palindrome or not.


#include<stdio.h>
#include<string.h>
int main() Output:
{ enter a string: abc
char s1[20],s2[20]; not a palindrome string
printf(“enter a string: ”);
scanf(“%s”,s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf(“string is a palindrome”);
else
printf(“not a palindrome string”);
return 0;
}

33. File Operations:


#include<stdio.h>
int main()
{ Output:
file *fp,*fp1; enter the contents of file1(#-end)
char c; good morning#
the contents of file2 good morning
fp=fopen(“test.c”,”w”);
printf(“\nenter the contents for file1(#.end)\
n”);
c=getchar();
while(c!=’#’)
{
fputc(c,fp);
c=getchar();
}

rewind(fp);
fp=fopen(“test.c”,”r”);
fp1=fopen(“tes.c”,”w”);
c=fgetc(fp);
while(c!=eof)
{
fputc(c,fp);
c=fgetc(fp);
}
fclose(fp);
fclose(fp1);

fp1=fopen(“tes.c”,”r”);
c=fgetc(fp1);
printf(“\nthe contents in file 2\n”);
while(c!=eof)
{
putchar(c);
c=fgetc(fp1);
}
fclose(fp1);
return 0;
}

34. Number Of Occurrences Of Vowels, Consonants, Words, Spaces And Special Characters In
The Given Statement.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main()
{
char s[100];
int vow=0,cons=0,spc=0,punc=0,l,i;

printf(“enter the statement\n”);


gets(s);

l=strlen(s);

for(i=0;i<l;i++)
{
if( isalpha(s[i]))
{
if(s[i]==’a’||s[i]==’e’||s[i]==’i’||s[i]==’o’||s[i]==’u’)
{ vow++;
}
else
{
cons++;
}
}
if(isspace(s[i])
{
spc++;
}
if(ispunct(s[i]))
{
punc++;
}
}
printf(“\nno.of words=%d”,spc+1);
printf(“\nno.of vowels=%d”,vow);
printf(“\nno.of consonants=%d”,cons);
printf(“\nno.of space=%d”,spc);
printf(“\nno.on special characters=%d”,punc);
return 0;
}
Output:
Enter the statement
*Nothing is impossible in the world.
No.of words=6
No.of vowels=10
No.of consonants=19
No.of space=5
No.of special characters=1

35. Write a program to create enumerated data type for 12 months. Display their values in
integer constants.
#include<stdio.h>
int main()
{
Enum month(Jan, Feb, Mar, Apr, May, June, July, Aug,Sep, Oct, Nov, Dec)
printf(“Jan=%d”, Jan);
printf(“Feb=%d”, Feb);
printf(“June=%d”, June);
printf(“Dec=%d”, Dec);
}

Output:
Jan =0
Feb=1
June=5
Dec=11

1. A program to evaluate the equation y=x n when n is a non-negative integer.


#include<stdio.h>
void main()
{ Output:
int count, n; Enter the values of x and n: 2.5 4
float x,y; X=2.500000; n=4; x to power n= 39.062500
printf(“Enter the values of x and n:”);
scanf(“%f%d”, &x,&n);
y=1.0;
count=1;
while(count<=n)
{
y=y*x;
count++;
}
printf(“x=%f; n=%d; x to power n=%f”, x,
n,y);
}

2. A program to print the multiplication table from 1*1 to 12*10.


#include<stdio.h>
#define COLMAX 10
#define ROWMAX 12
void main()
{
int row, column, y;
row=1;
printf(“MULTIPLICATION TABLE ”);
printf(“ ”);
do
{
column=1;
do
{
y=row*column;
printf(“%d”, y);
column=column +1;
} while(column<=COLMAX);
printf(“\n”);
row=row+1;
} while(row<=ROWMAX);
printf(“ ”)
}
Output:
MULTIPLICATION TABLE

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120

3. A class of n students take an annual examination in m subjects. A program to read the


marks obtained by each student in various subjects and to compute and print the total
marks obtained by each of them.
#include<stdio.h>
#define FIRST 360
# define SECOND 240
void main()
{
int n, m, i, j, roll number, marks, total;
printf(“Enter number of students and subjects”);
scanf(“%d%d”, &n,&m);

printf(“\n”);
for(i=1; i<=n; ++i)
{
printf(“Enter roll number:”);
scanf(“%d”, &roll_number);
total=0;
printf(“Enter marks of %d subjects for ROLL NO”, m, roll number);
for(j=1; j<=m; j++)
{
scanf(“%d”, &marks);
total=total+marks;
}
printf(“TOTAL MARKS =%d”, total);
if(total>=FIRST)
printf(“(First division)”);
else if (total>=SECOND)
printf(“(Second division)”);
else
printf(“(***FAIL***)”);
}
}

Output:
Enter number of students and subjects 3 6
Enter roll_number: 8701
Enter marks of 6 subjects for ROLL NO 8701 81 75 83 45 61 59
TOTAL MARKS =404 (First division)
Enter roll_number:8702
Enter marks of 6 subjects for ROLL NO 8702 51 49 55 47 65 41
TOTAL MARKS =308(Second division)
Enter roll_number: 8704
Enter marks of 6 subjects for ROLL NO 8704 40 19 31 47 39 25
TOTAL MARKS=201(*** FAIL ***)

4. The program illustrates the use of the break statement in a C program.


#include<stdio.h>
void main()
{
int m;
float x, sum, average;
printf(“This program computes the average of a set of computers”);
printf(“Enter values one after another”);
printf(“Enter a NEGATIVE number at the end”);
sum=0;
for(m=1;m<=1000; ++m)
{
scanf(“%f”,&x);
if(x<0)
break;
sum+=x;
}

average=sum/(float)(m-1);
printf(“\n”);
printf(“Number of values =%d”,m-1);
printf(“sum=%f”, sum);
printf(“Average=%f”, average);
}

Output:
This program computes the average of a set of numbers
Enter values one after another
Enter a NEGATIVE number at the end
21 23 24 22 26 22 -1
Number of values =6
Sum= 138.000000
Average=23.000000

5. Program illustrates the use of continue statement.


#include<stdio.h>
#include<math.h>
void main()
{
int count, negative;
double number, sqroot;

printf(“enter 9999 to STOP”);


count=0;
negative=0;

while(count<=100)
{
printf(“enter a number:”);
scanf(“%lf”, &number);
if(number==9999)
break;
if(number<0)
{
printf(“Number is negative ”);
negative++;
continue;
}
sqroot=sqrt(number);
printf(“Number=%lf square root=%lf ”, number, sqroot); count++;
}
printf(“Number of items done =%d”, count);
printf(“Negative items=%d”, negative);
printf(“END OF DATA”);
}
Output:
Enter 9999 to STOP

Enter a number: 25.0


Number=25.000000
Square root =5.000000

Enter a number: 40.5


Number =40.500000
Square root=6.363961

Enter a number:-9
Number is negative

Enter a number: 16
Number= 16.000000
Square root=4.000000

Enter a number: -14.75


Number is negative

Enter a number: 80
Number=80.000000
Square root=8.944272

Enter a number: 9999


Number of items done=4
Negative items=2
END OF DATA

6. Given below is the list of marks obtained by a class of 50 students in an annual examination.
43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 37 40 49 16 75 87 91 33 24 58 78
65 56 76 67 45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59

Write a program to count the number of students belonging to each of the following groups
of marks: 0-9,10-19,20 -29, ,100.
#include<stdio.h>
#define MAXVAL 50
#define COUNTER 11

void main()
{
float value [MAXVAL];
int i, low, high;
int group[COUNTER]={0,0,0,0,0,0,0,0,0,0,0};
for(i=0; i<MAXVAL; i++)
{
scanf(“%f”, &value[i]);
++group[(int)(value[i]/10)];
}
printf(“\n”);
printf(“GROUP RANGE FREQUENCEY”);
for(i=0; i<COUNTER; i++)
{
low=i*10;
if(i= =10)
high=100;
else
high=low+9;
printf(“%2d%3d to %3d%d”, i+1, low, high, group[i]);
}
}

7. Write a program for sorting the elements of an array in descending order.


#include<stdio.h>
int main()
{
int *arr, temp, i, j, n;
printf(“enter the number of elements in the array”);
scanf(“%d”, &n);
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
printf(“Elements of array in descending order are”); for(i=0; i<n; i++);
getch();
}

You might also like