Loading documents preview...
Set-1: Simple C Programs Program-1: Write a program to print “Hello World” on the screen. #include<stdio.h> void main() { printf("hello World\n"); } Output: hello World Program-2: Write a program to read one number and display it. #include<stdio.h> void main() { int num; printf("Enter a number"); scanf("%d",&num); printf("Number is %d",num); } Output: Enter a number4 Number is 4 Program-3: Write a program that will obtain the length and width of a rectangle from user and calculate its area, perimeter and diagonal. #include<stdio.h> #include<math.h> void main() { float l,b,a,p,d; printf("Enter l & b:"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); d=sqrt((l*l)+(b*b)); printf("Area is %f\n",a); printf("perimeter is %f\n",p); printf("diagonal is %f\n",d); } Output: Enter l & b:3 4 Area is 12.000000 perimeter is 14.000000 diagonal is 5.000000
Set-1 Program4: The distance between two cities in KM is input through keyboard. Write a program to convert and print this distance in meters, feet, inches, centimeters. #include<stdio.h> void main() { float km,m,cm,ft,inch; printf("Enter km:"); scanf("%f",&km); m=1000*km; cm=100000*km; ft=3281*km; inch=39370*km; printf("M is %f\n",m); printf("CM is %f\n",cm); printf("FT is %f\n",ft); printf("INCH is %f\n",inch); } Output: Enter km:5 M is 5000.000000 CM is 500000.000000 FT is 16405.000000 INCH is 196850.000000 Program5: Write a program to determine the salvage value of an item when the purchase price, years of service and the annual depreciation are given.
#include<stdio.h> void main() { float d,pp,sv; int y; printf("Enter the values of Purchase price,years of service and annual depreciation:\n"); scanf("%f%d%f",&pp,&y,&d); sv=pp-d*y; printf("Salvage value=%f\n",sv); } Output: Enter the values of Purchase price,years of service and annual depreciation: 200 1 100 Salvage value=100.000000
Set-1 Program6: Write a program to compute the area of the triangle given the values of a,b,c. #include<stdio.h> #include<math.h> void main() { int a,b,c,s; float area; printf("Enter the values of a,b & c:\n"); scanf("%d%d%d",&a,&b,&c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("Area of triangle is %f\n",area); } Output: Enter the values of a,b & c: 3 4 5 Area of triangle is 6.000000 Program7: Write a program to convert temperature in Celsius to Fahrenheit. #include<stdio.h> void main() { float c,f; printf("Enter celsius:"); scanf("%f",&c); f=1.8*c+32; printf("the value in fahrenheit is %f\n",f); } Output: Enter celsius:40 the value in fahrenheit is 104.000000 Program8: Write a program to find the sum of the digits of a 3-digit integer constant. #include<stdio.h> void main() { int a,b; printf("Please enter the three digit number"); scanf("%d",&a); b=(a/100)+(a%10)+((a/10)%10); printf("the sum of digits of the number is %d",b); } Output: Please enter the three digit number345 the sum of digits of the number is 12
Set-1 Program9: Write a program to reverse a digit. #include<stdio.h> void main() { int a,b; printf("Please enter the three digit number"); scanf("%d",&a); b=(a/100)+((a%10)*100)+((a/10)%10)*10; printf("reverse of the number is %d",b); } Output: Please enter the three digit number345 reverse of the number is 543 Program10: Write a program to interchange the values of two variables. #include<stdio.h> void main() { int x,y; printf("Enter the value of x:"); scanf("%d",&x); printf("Enter the value of y:"); scanf("%d",&y); x=x+y; y=x-y; x=x-y; printf("x is %d\n",x); printf("y is %d\n",y); } Output: Enter the value of x:3 Enter the value of y:4 x is 4 y is 3 Program11: Write a program to assign value of one variable to another using post and pre increment operator and print the results. #include<stdio.h> void main() { int a,b,c=4; a=++c; b=a++; c=++b; printf("a=%d b=%d c=%d",a,b,c); } Output: a=6 b=6 c=6
Set-1 Program12: Write a program to read the price of item in decimal form. #include<stdio.h> void main() { float r; int r1,p1; printf("Enter the amount:"); scanf("%f",&r); r1=r; p1=(r-r1)*100; printf("rupees are %d & paise is are %d\n",r1,p1); } Output: Enter the amount:235.45 rupees are 235 & paise is are 44
Program13: Write a program to convert days into months and days. #include<stdio.h> void main() { int m,d; printf("Enter the no of days:"); scanf("%d",&d); m=d/30; d=d%30; printf("No of months is %d months and %d days\n",m,d); } Output: Enter the no of days:234 No of months is 7 months and 24 days
Program14: Write a program that reads nos. from keyboard and gives addition, subtraction, multiplication, division and modulo. #include<stdio.h> void main() { int a,b; printf("Enter the value of a & b:"); scanf("%d%d",&a,&b); printf("Addition is %d+%d=%d\n",a,b,a+b); printf("Subtraction is %d-%d=%d\n",a,b,a-b); printf("Multiplication is %d*%d=%d\n",a,b,a*b); printf("division is %d/%d=%d\n",a,b,a/b); printf("Modulo is %d%%%d=%d\n",a,b,a%b); } Output: Enter the value of a & b:5 4 Addition is 5+4=9 Subtraction is 5-4=1 Multiplication is 5*4=20 division is 5/4=1 Modulo is 5%4=1
Set-2:C Programs using if, if-else, switch Program1: Write a program to read marks from keyboard and your program should display equivalent grade according to following table. Marks Grade 100-80 Distinction 60-79 First class 35-59 Second class 0-34 Fail
#include<stdio.h> void main() { int m; printf("Enter marks:"); scanf("%d",&m); if(m>=80&&m<100) printf("Distinction\n"); else if(m<80&&m>=60) printf("first class\n"); else if(m<60&&m>=35) printf("Second class\n"); else if(m<35&&m>=0) printf("Fail\n"); else printf("Invalid Input\n"); } Output: Enter marks:75 first class
Program2: Write a program for solution of quadratic equation. #include<stdio.h> #include<math.h> void main() { int a,b,c,D; float x1,x2; printf("Enter a b c:"); scanf("%d%d%d",&a,&b,&c); D=(b*b)-(4*a*c); if(D>0) { x1=(-b+sqrt(D))/(2*a); x2=(-b-sqrt(D))/(2*a); printf("x1=%f\tx2=%f",x1,x2); } else if(D==0) {x1=x2=-b/(2*a); printf("x1=x2=%f",x1); } else {printf("Roots are imaginary"); } } Output: Enter a b c:3 4
5 Roots are imaginary Program3: Make Simple Calculator using switch and if …else if. #include<stdio.h> void main() { int n1,n2,ch,ans; printf("Enter the value of n1 and n2\n and enter the choice"); scanf("%d%d%d",&n1,&n2,&ch); switch(ch) { case 1:ans=n1+n2; break; case 2:ans=n1-n2; break; case 3:ans=n1*n2; break; case 4:ans=n1/n2; break; case 5:ans=n1%n2; break; default: printf("wrong input"); } printf("Answer is %d",ans); } Output: Enter the value of n1 and n2 and enter the choice5 4 3 Answer is 20 Set-2 Program4: Find maximum and minimum of three numbers using ternary operator. #include<stdio.h> void main() { int a,b,c,max,min; printf("Enter three numbers"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) printf("max=%d",a); else printf("max=%d",c); } else { if(b>c) printf("Max=%d",b); else printf("Max=%d",c); } if(a
} else { if(b void main() { int year; printf("Enter year:\n"); scanf("%d",&year); if(year%4==0) printf("%d is a leap year\n",year); else printf("%d is not a leap year\n",year); } Output: Enter year: 2018 2018 is not a leap year Set-2 Program6: Convert the case of a given character. #include<stdio.h> #include void main() { char b; printf("Enter an alphabet"); putchar('\n'); b=getchar(); if(islower(b)) putchar(toupper(b)); else putchar(tolower(b)); } Output: Enter an alphabet y Y
Set-2 Program7: Write a program to find out Net salary, HRA, DA, PI of employee according to Basic salary. Do using if and also using switch statements. Basic salary HRA DA >=10000 20% 15% >=5000 and <10000 15% 10% <5000 10% 5% Net salary=Basic + HRA + DA #include<stdio.h> void main() { float bs,hra,da,net; printf("Enter the basic salary of employee"); scanf("%f",&bs); if (bs>=10000) { hra=0.2*bs; da=0.15*bs; net=bs+hra+da; printf("hra=%f\tda=%f\tnet=%f\t",hra,da,net); } else if(bs>=5000&&bs<10000) { hra=0.15*bs; da=0.1*bs; net=hra+da+bs; printf("hra=%f\tda=%f\tnet=%f\t",hra,da,net); } else if(bs<5000) { hra=0.1*bs; da=0.05*bs; net=hra+da+bs; printf("hra=%f\tda=%f\tnet=%f\t",hra,da,net); } else { printf("Invalid Input"); }} Output: Enter the basic salary of employee10000 hra=2000.000000 da=1500.000000 net=13500.000000
Set-2 Program8: The cost of one type of mobile service is Rs. 250 plus Rs. 1.25 for each call made over above 100 calls. Write a program to read costumer codes and calls made and print the bill for each customer. #include<stdio.h> void main() { int n,cc; float bill; printf("Enter customer code and calls:"); scanf("%d%d",&cc,&n); if(n>100) bill=250+1.25*(n-100); else bill=250; printf("Total Bill=%f",bill); } Output: Enter customer code and calls:2345 110 Total Bill=262.500000
Set-3 C Programs using for, while & do-while loop Program1: Write a program to print 1st to N natural numbers & calculate their sum & avg. #include<stdio.h> void main() { int n,i=1; float sum=0.00,avg; printf("Enter n:\n"); scanf("%d",&n); while(i<=n) { printf("%d\n",i); sum=sum+i; i++; } avg=sum/n; printf("Sum is %f",sum); printf("Avg is %f",avg); } Output: Enter n: 5 1 2 3 4 5 Sum is 15.000000Avg is 3.000000 Set-3 Program2: Write a program to print squares & cubes of 1st N natural numbers & calculate their sum & avg. #include<stdio.h> void main() { int i=1,n,s,c; float sums=0,avgs=0,sumc=0,avgc=0; printf("enter a number till which you want to print:"); scanf("%d",&n); printf("number\t \t square\t \t cube \n"); while(i<=n) { s=i*i; c=i*i*i; printf("%d \t \t %d \t \t %d \n",i,s,c); sums=sums+s; sumc=sumc+c; i=i+1; } avgs=sums/n; avgc=sumc/n; printf("sum \t %f \t %f \n",sums,sumc); printf("avg \t %f \t %f \n",avgs,avgc); }
Output:
enter a number till which you want to print:5 number square cube 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 sum 55.000000 225.000000 avg 11.000000 45.000000 Set-3 Program:3 Write a program to print all numbers between –n & +n. #include<stdio.h> void main() { int n,i; printf("Enter n:\n"); scanf("%d",&n); i=-n; while(i>=-n && i<=0) { printf("%d\n",i); i++; } i=1; while(i<=n) { printf("%d\n",i); i++; } } Output: Enter n: 5 -5 -4 -3 -2 -1 0 1 2 3 4 5
Set-3 Program4: Write a program to print 1st N odd & even numbers & calculate their sum & avg. #include<stdio.h> void main() { int n,i,b,sum; float avg; printf("enter the value n:"); scanf("%d",&n); b=0; sum=0; avg=1; printf("your odd numbers are\n"); for(i=1;i<=n;i++) { if(i%2!=0) { printf("%d\n",i); sum=sum+i; b=b+1; } } avg=sum/b; printf("sum=%d,Avg=%f\n",sum,avg); b=0; sum=0; avg=1; printf("your even numbers are\n"); for(i=1;i<=n;i++) { if(i%2==0) { printf("%d\n",i); sum=sum+i; b=b+1; } } avg=sum/b; printf("sum=%d,Avg=%f\n",sum,avg); } Output: enter the value n:10 your odd numbers are 1 3 5 7 9 sum=25,Avg=5.000000 your even numbers are 2 4 6 8 10 sum=30,Avg=6.000000
Set-3 Program5: Write a program to print all numbers between given two numbers x & y including x & y,& calculate their sum & avg. #include<stdio.h> void main() { int x,y,n=0; float avg,sum=0; printf("enter first no.:\n"); scanf("%d",&x); printf("enter second no.:\n"); scanf("%d",&y); while(x<=y) { printf("%d\n",x); sum=sum+x; x++; n++; } printf("sum is %f\n",sum); avg=sum/n; printf("avg is %f\n",avg); } Output: enter first no.: 3 enter second no.: 10 3 4 5 6 7 8 9 10 sum is 52.000000 avg is 6.500000
Set-3 Program6: Write a program to print all odd & even numbers between given two numbers x & y including x & y,& their sum & avg. #include<stdio.h> void main() { int x,y,i,b,sum; float avg; printf("enter x:"); scanf("%d",&x); printf("enter y:"); scanf("%d",&y); b=0; sum=0; avg=1; printf("your odd numbers are\n"); for(i=x;i<=y;i++) { if(i%2!=0) { printf("%d\n",i); sum=sum+i; b=b+1; } } avg=sum/b; printf("sum=%d , Avg=%f\n",sum,avg); b=0; sum=0; avg=1; printf("your even numbers are\n"); for(i=x;i<=y;i++) { if(i%2==0) { printf("%d\n",i); sum=sum+i; b=b+1; } } avg=sum/b; printf("sum=%d , Avg=%f\n",sum,avg); } Output: enter x:10 enter y:20 your odd numbers are 11 13 15 17 19 sum=75 , Avg=15.000000 your even numbers are 10 12 14 16 18 20 sum=90 , Avg=15.000000
Set-3 Program7: Write a program to print every third number beginning from 2 untill number<100, & calculate their sum & avg. #include<stdio.h> void main() { int i,sum=0,c=0; float avg; for(i=2;i<100;i=i+3) { printf("\nthe numbers are : %d",i); sum+=i; c++; } printf("\nthe sum is %d",sum); avg=sum/c; printf("\navg is %f",avg); } Output: the numbers are : 2 the numbers are : 5 the numbers are : 8 the numbers are : 11 the numbers are : 14 the numbers are : 17 the numbers are : 20 the numbers are : 23 the numbers are : 26 the numbers are : 29 the numbers are : 32 the numbers are : 35 the numbers are : 38 the numbers are : 41 the numbers are : 44 the numbers are : 47 the numbers are : 50 the numbers are : 53 the numbers are : 56 the numbers are : 59 the numbers are : 62 the numbers are : 65 the numbers are : 68 the numbers are : 71 the numbers are : 74 the numbers are : 77 the numbers are : 80 the numbers are : 83 the numbers are : 86 the numbers are : 89 the numbers are : 92 the numbers are : 95 the numbers are : 98 the sum is 1650 avg is 50.000000
Set-3 Program8: Write a program to print all numbers exactly divisible by 5 until number < 100,& calculate their sum & avg. Use modulus operator to check divisibility. #include<stdio.h> void main() { int i=0,n=0; float avg,sum=0; while(i<100) { if(i%5==0) { printf("%d\t",i); sum=sum+i; n++; } i++; } avg=sum/n; printf("sum is %f and avg is %f\n",sum,avg); } Output: 0
5 75
10 80
15 85
20 90
25 95
30 35 40 45 50 sum is 950.000000 and avg is 47.500000
55
Program9: Write a program to print following series: -15,-10,-5,0,5,10,15. #include<stdio.h> void main() { int a; a=0; for(a=-15;a<=15;a=a+5) { printf("%d\t",a); } } Output: -15
-10
-5
0
5
10
15
Set-3 Program10: Write a program to print the value of the following series: -1,x,-x^2,x^3,-x^4…etc. #include<stdio.h> #include<math.h> void main() { int x,i,n,ans; printf("\n enter x & n\n"); scanf("%d %d",&x,&n); for(i=0;i<=n;i++) { ans=pow(x,i) *pow(-1,i+1); printf("%d\n",ans); } } Output:
60
65
70
enter x & n 2 5 -1 2 -4 8 -16 32 Program11: Write a program to print the multiplication table (i.e X*1=X). #include<stdio.h> void main() { int i,j; for(i=1;i<=10;++i) { for(j=1;j<=10;++j) { printf("%d*%d=%d\t",j,i,i*j); } printf("\n"); } } Output: 1*1=1 2*1=2 3*1=3 4*1=4 5*1=5 6*1=6 7*1=7 8*1=8 9*1=9 10*1=10 1*2=2 2*2=4 3*2=6 4*2=8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 10*2=20 1*3=3 2*3=6 3*3=9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27 10*3=30 1*4=4 2*4=8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36 10*4=40 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45 10*5=50 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54 10*6=60 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63 10*7=70 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72 10*8=80 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 10*9=90 1*10=10 2*10=20 3*10=30 4*10=40 5*10=50 6*10=60 7*10=70 8*10=80 9*10=90 10*10=100
Set-3 Program12: Write a program to calculate the power of a number without using pow() function. #include<stdio.h> void main() { int base,exp; int result=1; printf("Enter a base number:"); scanf("%d",&base); printf("Enter an exponent"); scanf("%d",&exp); while(exp!=0) { result*=base; --exp; } printf("Answer =%d",result); }
Output: Enter a base number:2 Enter an exponent2 Answer =4 Program13: Write a program to calculate the factorial of a number. #include<stdio.h> void main() { float x,f=1; printf("enter no.:\n"); scanf("%f",&x); while(x>0) { f=f*x; x--; } printf("factorial is %f\n",f); } Output: enter no.: 5 factorial is 120.000000 Set-3 Program14: Write a program to print all letters of the alphabet in upper & lower case. #include<stdio.h> void main() { int i; for(i=65;i<91;i++) { printf("%c",i); } printf("\n"); for(i=97;i<123;i++) { printf("%c",i); } } Output: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz Program15: Write a program to print all characters between given 2 numbers x & y. #include<stdio.h> void main() { int i,x,y; printf("Enter x and y:"); scanf("%d %d",&x,&y); for(i=x;i<=y;i++) { printf("%d",i); printf("\t"); }
} Output: Enter x and y:2 5 2 3
4
5
Set-3 Program16: Write a program to print 1st N numbers of the Fibonacci series. #include<stdio.h> void main() { int n,count=3,c,a=0,b=1; printf("enter n:\n"); scanf("%d",&n); printf("%d\n",a); printf("%d\n",b); while(count<=n) { c=a+b; printf("%d\n",c); a=b; b=c; count++; } } Output: enter n: 4 0 1 1 2
Program17: Write a program to check if the given number is prime or not. #include<stdio.h> void main() { int n,i,c=0; printf("Enter any number\n"); scanf("%d",&n); for(i=1;i<=n;i++) { if(n%i==0) { c++; } } if(c==2) { printf("n is a Prime number"); } else { printf("n is not a Prime number"); } }
Output: Enter any number 6 n is not a Prime number
Set-3 Program18: Write a program for 1-x+x^2/2!-x^3/3!+x^4/4….v^n/n! terms. #include<stdio.h> #include<math.h> void main() { float x,sum,term; int i,n; printf("Enter the value of x and (n) Number of term to be sum \t:"); scanf("%f%d",&x,&n); sum=1; term=1; for(i=1;i
:2
Set-4: C Programs for Pattern Generation 1 12 123 1234 #include<stdio.h> void main() { int i,j,n; printf("enter the number of rows in triangle"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("%d ",j); } printf("\n"); } } 1 22 333 4444 #include<stdio.h> void main() { int i,j,n; printf("enter the number of rows in triangle"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("%d ",i); } printf("\n"); } }
Set-4 1 121 12321 1234321 #include<stdio.h> void main() { int i,j,k,n; printf("enter the no. of rows"); scanf("%d",&n); for(i=1;i<=n;i++) { for(k=1;k<=n-i;k++) { printf(" "); } for(j=1;j<=i;j++) { printf("%d ",j); } for(j=i-1;j!=0;j--) { printf("%d ",j); } printf("\n"); } } GHIJ DEF BC A #include<stdio.h> void main() { int i,j,n,b=1; char a=65; printf("enter the no of rows"); scanf("%d",&n); for(i=n;i>=0;i--) { for(j=1;j<=i;j++) { printf("%c ",a+2*(n-1)); a++; } a=a-(i+n)+b; b++; printf("\n"); } }
Set-4
1 01 101 0101 #include<stdio.h> void main() { int i=1,n,j; printf("enter a number"); scanf("%d",&n); while(i<=n) {j=1; if (i%2!=0) { while(j<=i) { if (j%2!=0) { printf("%d ",1); } else { printf("%d ",0); } j++; } i++; printf("\n"); } else { while(j<=i) { if (j%2!=0) { printf("%d ",0); } else { printf("%d ",1); } j++; } i++; printf("\n"); } } }
Set-4 * ** *** **** #include<stdio.h> void main() { int n,i,j; printf("enter the number of rows"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) {printf("* "); } printf("\n"); } } AAAAA BBBB CCC DD E #include<stdio.h> void main() { int n,i,j; char a=65; printf("enter the number of rows\n"); scanf("%d",&n); for(i=n;i!=0;i--) { for(j=i;j!=0;j--) { printf("%c ",a); } a++; printf("\n"); } }
Set-5: C Programs using an Array Program1: Write a program to read N integers and print N integers using an array. #include <stdio.h> void main() { int arr[10]; int i; printf("Input 10 elements in the array :\n"); for(i=0; i<10; i++) { printf("element - %d : ",i); scanf("%d", &arr[i]); } printf("\nElements in array are: "); for(i=0; i<10; i++) { printf("%d ", arr[i]); } printf("\n"); } Output: Input 10 elements in the array : element - 0 : 3 element - 1 : 5 element - 2 : 2 element - 3 : 6 element - 4 : 1 element - 5 : 7 element - 6 : 8 element - 7 : 9 element - 8 : 0 element - 9 : 3 Elements in array are: 3 5 2 6 1 7 8 9 0 3
Set-5 Program2: Write a program to find the smallest and largest number in an array of N integers. #include<stdio.h> int main() { int a[50],i,n,large,small; printf("How many elements:"); scanf("%d",&n); printf("Enter the Array:"); for(i=0;ilarge) large=a[i]; if(a[i]<small) small=a[i]; } printf("The largest element is %d",large); printf("\nThe smallest element is %d",small); return 0; } Output: How many elements:5 Enter the Array:1 2 3 4 5 The largest element is 5 The smallest element is 1
Set-5 Program3: Write a program to arrange an array of N elements into ascending order. #include <stdio.h> void main() { int i, j, a, n, number[30]; printf("Enter the value of N \n"); scanf("%d", &n); printf("Enter the numbers \n"); for (i = 0; i < n; ++i) scanf("%d", &number[i]); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { if (number[i] > number[j]) { a = number[i]; number[i] = number[j]; number[j] = a; } } } printf("The numbers arranged in ascending order are given below \n"); for (i = 0; i < n; ++i) printf("%d\n", number[i]); } Output: Enter the value of N 6 Enter the numbers 3 4 2 6 7 1 The numbers arranged in ascending order are given below 1 2 3 4 6 7
Set-5 Program4: Write a program to arrange an array of N elements into descending order. #include <stdio.h> void main () { int number[30]; int i, j, a, n; printf("Enter the value of N\n"); scanf("%d", &n); printf("Enter the numbers \n"); for (i = 0; i < n; ++i) scanf("%d", &number[i]); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { if (number[i] < number[j]) { a = number[i]; number[i] = number[j]; number[j] = a; } } } printf("The numbers arranged in descending order are given below\n"); for (i = 0; i < n; ++i) { printf("%d\n", number[i]); } } Output: Enter the value of N 5 Enter the numbers 4 3 2 1 6 The numbers arranged in descending order are given below 6 4 3 2 1
Set-5 Program6: Write a program to insert value at ith location or value entered by user using one dimensional array. #include<stdio.h> void main() { int arr[30], element, num, i, location; printf("\nEnter no of elements :"); scanf("%d", &num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } printf("\nEnter the element to be inserted :"); scanf("%d", &element); printf("\nEnter the location"); scanf("%d", &location); for (i = num; i >= location; i--) { arr[i] = arr[i - 1]; } num++; arr[location - 1] = element; for (i = 0; i < num; i++) printf("n %d", arr[i]); return (0); } OUTPUT: Enter no of element:2 4 5 Enter no to be inserted:6
Enter the location:2 n 4n 6n 5n
Set-5 Program7: Write a program to delete value at ith location or value entered by user using one dimensional array. include<stdio.h> int main() { int arr[30], num, i, loc; printf("\nEnter no of elements :"); scanf("%d", &num); printf("\nEnter %d elements :", num); for (i = 0; i < num; i++) { scanf("%d", &arr[i]); } printf("\n location of the element to be deleted :"); scanf("%d", &loc); while (loc < num) { arr[loc - 1] = arr[loc]; loc++; } num--; for (i = 0; i < num; i++) printf("\n %d", arr[i]); return (0);} OUTPUT: Enter the no of element:2 Enter element:4 5 Enter the location to be deleted:2 4
Set 5 Program 8: #include <stdio.h> int main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for (c = 0; c < m; c++) for (d = 0 ; d < n; d++) scanf("%d", &second[c][d]); printf("Sum of entered matrices:-\n"); for (c = 0; c < m; c++) { for (d = 0 ; d < n; d++) { sum[c][d] = first[c][d] + second[c][d]; printf("%d\t", sum[c][d]); } printf("\n"); } return 0; } Output: Enter no of row and colomns:3 3 Enter the element of first matrix:1 2 3
4 5 6 7 8 9 Enter element of 2nd matrix:1 2 3 4 5 6 7 8 9 Sum of matrices: 2
4
6
8 10 12 14 16 18
Set-5 Program9: Write a program to check whether given 3*3 matrices is magic square or not.
#include<stdio.h> int main() { int size = 3, row, column = 0, sum, sum1, sum2; int matrix[3][3]; int flag = 0; printf("\nEnter matrix : "); for (row = 0; row < size; row++) { for (column = 0; column < size; column++) scanf("%d", &matrix[row][column]); } printf("Entered matrix is : \n"); for (row = 0; row < size; row++) { printf("\n"); for (column = 0; column < size; column++) { printf("\t%d", matrix[row][column]); } } sum = 0; for (row = 0; row < size; row++) { for (column = 0; column < size; column++) { if (row == column) sum = sum + matrix[row][column]; } } for (row = 0; row < size; row++) { sum1 = 0; for (column = 0; column < size; column++) { sum1 = sum1 + matrix[row][column];
} if (sum == sum1) flag = 1; else { flag = 0; break; } } for (row = 0; row < size; row++) { sum2 = 0; for (column = 0; column < size; column++) { sum2 = sum2 + matrix[column][row]; } if (sum == sum2) flag = 1; else { flag = 0; break; } } if (flag == 1) printf("\nMagic square"); else printf("\nNo Magic square"); return 0; } OUTPUT: Enter matix:2 7 6
9 5 1 4 3 8 276 951 438 It is magic matrix
Set-06 :C Programs for String Operation
Program1:Write a Programme to read string from the user and print the length of string.
#include <stdio.h> #include <string.h> voidmain() { charstring[30]; int i, n; printf("Enter number of strings to input\n"); scanf("%d",&n); printf("Enter Strings one by one: \n"); for(i=0; i< n ; i++) { scanf("%s",string[i]); } printf("The length of each string: \n"); for(i=0; i< n ; i++) { printf("%s",string[i]); printf("%d\n",strlen(string[i])); }
}
Output: Enter number of strings to input 3 Enter Strings one by one: HELLO BVM VVNAGAR The length of each string: HELLO 5 BVM 3 VVNAGAR 7
Set-6 Program2 : Write a Programme to count number of words in given string. #include <stdio.h> #include <string.h> void main() { char s[200]; int count =0, i; printf("enter the string\n"); scanf("%[^\n]s", s); for(i =0;s[i]!='\0';i++) { if(s[i]==' ') count++; } printf("number of words in given string are: %d\n", count +1); } Output: Enter the String: BIRLA VISHWAKARMA MAHAVIDYALAYA Number of words in given string are : 3
Set-6 Program3: Write a program to Reverse a String #include <stdio.h> #include <string.h> int main() { charStr[100], RevStr[100]; int i, j, len; printf(“ Enter any String : "); gets(Str); j = 0; len = strlen(Str); for (i = len - 1; i >= 0; i--) { RevStr[j++] = Str[i]; } RevStr[i] = '\0'; printf("\n String after Reversing = %s", RevStr); return 0; }
Output: Enter any String : HELLO
String after reversing=OLLEh Set-6 Program4: Write a Program to Check the given string is Palindrome or not * #include <stdio.h> #include <string.h> int main() { charstr[100]; int i, len, flag; flag = 0; printf("\n Enter any String : "); gets(str); len = strlen(str); for(i = 0; i
Out Put: Enter a string : MALAYALAM MALAYALAM is Palindrome String.
Set-6 Program5 :Write a Program to find ASCII Value of a Character */ #include <stdio.h> void main() { charch; printf("\n Enter any character \n"); scanf("%c",&ch); printf("\n The ASCII value of given character = %d",ch); return 0; }
Output: Enter any character a The ASCII value of given character=97 Set-6 Program6 :Write a program to count all occurrences of a character in a given string #include <stdio.h> #define MAX_SIZE 100 int main() { charstr[MAX_SIZE]; chartoSearch; int i, count; printf("Enter any string: "); gets(str); printf("Enter any character to search: "); toSearch = getchar(); count = 0; i=0; while(str[i] != '\0') { /* * If character is found in string then * increment count variable */ if(str[i] == toSearch) { count++; } i++; } printf("Total occurrence of '%c' = %d", toSearch, count); return 0; }
Output: Enter any string:wow Enter any character to search: w Total occurrence of 'w' = 2 Set-6 Program7 : Write a program to count total number of words in a string #include <stdio.h> #define MAX_SIZE 100 int main() { charstr[MAX_SIZE]; charprevChar; int i, words; /* Input string from user */ printf("Enter any string: "); gets(str); i = 0; words = 0; prevChar = '\0'; while(1) { if(str[i]==' ' || str[i]=='\n' || str[i]=='\t' || str[i]=='\0') { if(prevChar != ' ' &&prevChar != '\n' &&prevChar != '\t' &&prevChar != '\0') { words++; } } if(str[i] == '\0') break; else i++; } printf("Total number of words = %d", words); return 0; } Output: Enter any string: Birla VishwakarmaMahavidhyalaya Total number of words = 3
Set-6 Program8: Write a program to join two strings
#include <stdio.h> int main() { char str1[50], str2[50], i, j; printf("\nEnter first string: "); scanf("%s",str1); printf("\nEnter second string: "); scanf("%s",str2); for(i=0; str1[i]!='\0'; ++i); for(j=0; str2[j]!='\0'; ++j, ++i) { str1[i]=str2[j]; } str1[i]='\0'; printf("new string %s",str1); return 0; } Output: Enter the first string : Hello Enter the second string :Bvm New string :HelloBvm
Set-6 Program9: Write a program to join two strings. #include <stdio.h> int main() { char s1[100], s2[100], i; printf("Enter string s1: "); scanf("%s",s1); for(i = 0; s1[i] != '\0'; ++i) { s2[i] = s1[i]; } s2[i] = '\0'; printf("String s2: %s", s2); return 0; } Output: Enter String s1: programiz String s2: programiz
Set 7: Program using functions 1.//WAP to find number is odd or not #include<stdio.h> Void odd(); Void main() { int a; Printf (“enter the number”); Scanf (“%d”&a); Odd(); } Void odd() {int a; If(a%2==0) { printf (“number is not odd”); } Else { printf (“number is odd”);
}
2. //WAP to find out factorial of given number using function #include<stdio.h> long factorial(int); int main() { int n; long f; printf("Enter an integer to find its factorial\n"); scanf("%d", &n); if (n < 0) printf("Factorial of negative integers isn't defined.\n"); else { f = factorial(n); printf("%d! = %ld\n", n, f); } return 0; } long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1));} 3. //WAP to generate series x, x^2,x^3…. #include #include<math.h> Int series(int n); void main() { inti,n; float x,sum=0;
printf (“enter the value of x”); scanf (“%f”,&x); series(n); printf(“1+x+x^2+……+x^n”); for(i=1;i<=n;++i) sum+=pow(x,i); sum++; printf(“nSum=%f”,sum); } int series(int n) { int Printf (“nnEnter the value of x and n:”); scanf (“%d”,&n); return(n); } 4. //WAP to find maximum of 3 numbers #include<stdio.h> Int large(int a, intb,int c) {int l=0; If(a>b&&a>c) {l=a;} Elseif(b>c&&b>a) {l=b;} Else {l=c;} return l; } Int main() { inta,b,c; printf (“enter 3 numbers”); scanf (“%d %d %d”,&a,&b,&c); printf (“largest number is%d”largest(a,b,c)); return 0;
}
5. //WAP to find x^y using functions #include<stdio.h> #include<math.h> int power(int x, int y); Void main() {intx,y,z; Printf (“enter the value of x and y”); Scanf (“%d %d”,&x,&y); Z=power(x,y); Printf (“ans is %d”,z); } int pow(intx,int y) {int p; P=pow(x,y); Return p; } 6. //WAP that used user defined function swap() and interchange the value of 2 variables #include <stdio.h> void swap(int*, int*); //Swap function declaration int main() { int x, y printf("Enter the value of x and y\n"); scanf("%d%d",&x,&y); printf("Before Swapping\nx = %d\ny = %d\n", x, y); swap(&x, &y); printf("After Swapping\nx = %d\ny = %d\n", x, y); return 0;
} //Swap function definition void swap(int *a, int *b) { int t; t = *b; *b = *a; *a = t; }
7. Write a calculator program using user defined function for each. #include <stdio.h> /** * Function declarations for calculator */ floatadd(float num1,float num2); floatsub(float num1,float num2); floatmult(float num1,float num2); floatdiv(float num1,float num2); intmain() { char op; float num1, num2, result=0.0f; /* Print welcome message */ printf("WELCOME TO SIMPLE CALCULATOR\n"); printf("----------------------------\n"); printf("Enter [number 1] [+ - * /] [number 2]\n"); /* Input two number and operator from user */ scanf("%f %c %f",&num1,&op,&num2); switch(op) { case'+': result =add(num1, num2); break; case'-': result =sub(num1, num2); break; case'*': result =mult(num1, num2); break; case'/': result =div(num1, num2); break; default: printf("Invalid operator"); } /* Print the result */ printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
return0; }
/** * Function to add two numbers */ floatadd(float num1,float num2) { return num1 + num2; } /** * Function to subtract two numbers */ floatsub(float num1,float num2) { return num1 - num2; } /** * Function to multiply two numbers */ floatmult(float num1,float num2) { return num1 * num2; } /** * Function to divide two numbers */ floatdiv(float num1,float num2) { return num1 / num2; }
Set -9 : C Programs using Pointers Program1: Write a program to print the address of different data type variables along with its value.
#include<stdio.h> void main() { int a; float b; char c; int *ptr_a=&a; float *ptr_b=&b; char *ptr_c=&c; printf(“%d is stored at address %u.\n”,a,&a); printf(“%f is stored at address %u.\n”,b,&b); printf(“%c is stored at address %u.\n”,c,&c); } Output: a is stored in 1024 b is stored in 1025 C is stored in 1026
Set-9
Program2: Write a program using a pointer to read in a array of integers and print its element in reverse order. #include<stdio.h> void main() { int size, i, arr[20]; int *ptr; clrscr(); ptr = &arr[0]; printf("\nEnter the size of array : "); scanf("%d", &size);
printf("\nEnter %d integers into array: ", size); for (i = 0; i< size; i++) { scanf("%d", ptr); ptr++; }
ptr = &arr[size - 1];
printf("\nElements of array in reverse order are :");
for (i = size - 1; i>= 0; i--) { printf("\nElement%d is %d : ", i, *ptr); ptr--; } } Output: Enter the size of array: 5 Enter 5 integers into array 1 2 3 4 5 Elements of array in reverse order are: 5 4 3
2 1 Set-9
Program3: Write a program that compares two integer arrays to see whether they are identical.
#include<studio.h> Void main() { int a[15],b[15],k=0;i; printf(“enter the integer array 1:”); for(i=0;i<15;i++) { scanf(“%d”,a[i]); } printf(“Enter the integer array 2:”); for(i=0;i<15;i++) { scanf(“%d”,b[i]); }
for(i=0;i<=15;i++) { if(a[I]==b[I]) k++; } if(k==5) Printf(“\n identical”); else printf(“\n not identical”); } Output: Enter the integer array 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Enter the integer array 2: 16 17 18 19 20 21 21 22 23 24 25 26 27 28 29 30
NOT IDENTICAL
Set-9
Program4: Write a program using pointer to sort a array in ascending or descending order. #include <stdio.h> void main() { int *a,i,j,temp,n[20];
printf(" Enter the no. of elements in a array (max 20): "); scanf("%d",&n);
printf(" Input %d number of elements in the array : \n",n); for(i=0;i *(a+j)) { temp = *(a+i); *(a+i) = *(a+j); *(a+j) = temp; } } } printf("\n The elements in the array after sorting : \n"); for(i=0;i
Elenent-15 1 2 3 15 16 Set-9
Program 5: Write a program using pointer to copy one string to another string.
#include<stdio.h> voidcopystr(char*,char*); void main() { char*str1="CPU is easy"; char str2[30]; clrscr(); copystr(str2,str1); printf("\n %s",str2); getch(); } voidcopystr(char *dest, char *src) { while(*src!='\0') *dest++=*src++; *dest='\0'; return; }
Output: Cpu is easy
Cpu is easy