C Programs
C Programs
#include<stdio.h> main() { int a; printf(enter first value \n); scanf(%d, &a); int b; printf(enter second value \n); scanf(%d, &b); int c; c=a+b; printf(\n sum=%d, c); getch(); }
2.
#include<stdio.h> #include<conio.h> int main() { int a,b,c; float d; clrscr(); printf("Enter Three numbers \n"); scanf("%d%d%d",&a,&b,&c); d=(a+b+c)/3; printf("Average is = %f",d); getch(); return 0; }
3.
#include<stdio.h> void main() { int year; printf("Enter any year: "); scanf("%d",&year); if(((year%4==0)&&(year%100!=0))||(year%400==0)) printf("%d is a leap year",year); else printf("%d is not a leap year",year); }
4.
#include<stdio.h> #include<conio.h> void main(); { int s; clrscr(); printf(enter the number); scanf(%d,&s); if(s==0) printf(%d is equal to zero,s); else if(s>0) printf(%d is positive,s); else printf(%d is negative,s); getch(); }
5.
#include<stdio.h> #include<conio.h> void main() { int i,j,c,n; clrscr(); printf(enter the limit); scanf(%d,&n); for(i=0;i<n;i++) { c=0; for(j=1;j<=i;j++) { if(i%j==0) { c++; }} if(c==2) { printf(%d\t,i); }} getch(); }
OUTPUT: Enter the limit 100 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
6.
#include<stdio.h> void main() { int num,sum=0,r; printf("Enter a number: "); scanf("%d",&num); while(num) { r=num%10; num=num/10; sum=sum+r; } printf("Sum of digits of number: %d",sum); }
7.
#include<stdio.h> #include<conio.h> void main() { int odd(int); int i,n; clrscr(); printf("\n Enter the limit:"); scanf("%d",&n); printf("\n Odd nos.upto %d is \n"); odd(n); getch(); } odd(int n) { int i; for(i=0;i<n;i++) { if(i%2!=0) { printf("\t%d\t",i); } } return 0; }
OUTPUT:
8.
PALINDROME OR NOT
#include<conio.h> #include<stdio.h> void main() { int i,ch,rem,rev=0,c=0,no; clrscr(); printf("menu:\n1.odd or even\n2.prime or not\n3.palindrome or not\n4.exit\n\nenter your choice"); scanf("%d",&ch); do { switch(ch) { case 1: printf("\nenter the number"); scanf("%d",&no); if(no%2==0) { printf("\nthe number is even"); } else { printf("\nthe number is odd"); } break; case 2: printf("\nenter the number"); scanf("%d",&no);
8
for(i=1;i<=no;i++) { if(no%i==0) { c=c+1; } } if(c<=2) { printf("\nthe number is prime"); } else { printf("\nthe number is not prime"); } break; case 3: printf("\nenter the number"); scanf("%d",&no); for(i=no;i>0;i=i/10) { rem=i%10; rev=rev*10+rem; } if(rev==no) { printf("\nthe number is a palindrome"); } else { printf("\nthe number is not a palindrome");
9
} break; case 4: exit(0); default: printf("\nyour selection is invalid"); break; } printf("\nenter your choice"); scanf("%d",&ch); }while(ch<=4); getch(); }
OUTPUT:
MENU: 1.Odd or even 2.Prime or not 3.Palindrome or not 4.Exit The number is not prime Enter your choice3 Enter the number121 The number is a palindrome Enter your choice3 Enter the number123 The number is not a palindrome Enter your choice4
Enter your choice1 Enter the number6 The number is even Enter your choice1 Enter the number3 The number is odd Enter your choice2 Enter the number7 The number is prime Enter your choice2 Enter the number4
10
9.
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int dec,s=0,a=1,r,ch,bi,i,re=1,dec1,s1=0,a1=1,r1,s2=0; clrscr(); printf("\n menu driven program "); printf("\n 1.decimal to binary"); printf("\n 2.decimal to octal"); printf("\n 3.binary to decimal"); printf("\n 4.exit"); do { printf("\n enter your choice(1,2,3,4):"); scanf("%d",&ch); switch(ch) { case 1: printf("\n enter a decimal number:"); scanf("%d",&dec); while(dec>0) { r=dec%2; s=s+(a*r); dec=dec/2; a=a*10; } printf("\n binary is %d",s);
11
break; case 2: printf("\n enter a decimal number:"); scanf("%d",&dec1); while(dec1>0) { r1=dec1%8; s1=s1+(a1*r1); dec1=dec1/8; a1=a1*10; } printf("\n octal is %d",s1); s1=0;a1=1; break; case 3: printf("\n enter a binary number:"); scanf("%d",&bi); i=0; while(bi>0) { re=bi%10; s2=s2+(re*pow(2,i)); bi=bi/10; i++; } printf("\n decimal is %d",s2); break; case 4: exit(); } }
12
while(ch<=4); getch(); }
OUTPUT:
MENU DRIVEN PROGRAM 1.Decimal to Binary 2.Decimal to Octal 3.Binary to Decimal 4.Exit Enter your choice(1,2,3,4):1
13
10.
REVERSE OF A NUMBER
#include<stdio.h> #include<conio.h> void main() { int a=0,rem=0,rev=0; clrscr(); printf("\n enter a no:"); scanf("%d",&a); while(a>0) { rem=a%10; rev=rev*10+rem; a=a/10; } printf(" the reverse no is: %d",rev); getch(); }
OUTPUT:
14
11.
AMSTRONG
#include<stdio.h> #include<conio.h> void main() { int s=0,n,b,i; clrscr(); printf("enter a number"); scanf("%d",&n); for(i=n;i>0;i=i/10) { b=i%10; s=s+(b*b*b); } if(n==s) { printf("no is amstrong"); } else { printf("no not amstrong"); } getch(); }
15
12.
MULTIPLICATION TABLE
#include<stdio.h> void main() { int r,i,j,k; printf("Enter the number range: "); scanf("%d",&r); for(i=1;i<=r;i++) { for(j=1;j<=10;j++) printf("%d*%d=%d \n",i,j,i*j); printf("\n"); } }
OUTPUT:
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
16
13.
SEQUENTIAL SEARCH
#include<stdio.h> #include<conio.h> void main() { int a[100],l,k,i,c=0; clrscr(); printf("enter the limit of array: "); scanf("%d",&l); printf("enter the array elements: "); for(i=0;i<l;i++) { scanf("%d",&a[i]); } printf("enter the number to search: "); scanf("%d",&k); for(i=0;i<l;i++) { if(a[i]==k) { c=c+1; break; } } if(c>=1) { printf("the number is found in the position: %d ",i+1); } else {
17
OUTPUT:
Enter the limit of array: 4 Enter the Array elements: 6 7 8 9 Enter the number to search: 8 The number is found in the position: 3
18
14.
BUBBLE SORT
#include<stdio.h> #include<conio.h> void main() { int n,i,a[20],temp; clrscr(); printf(enter the limit); scanf(%d,&n); printf(\nenter the elements); for(i=0;i<n;i++) { scanf(%d,&a[i]); } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; }}} printf(sorted array); for(i=0;i<n;i++) { printf(\t); printf(%d,a[i]); }
19
getch(); }
OUTPUT:
20
15.
BINARY SEARECH
#include<stdio.h> #include<conio.h> void main() { int a[20],i,f,j,n,key,mid,low,high; clrscr(); printf(enter the limit); scanf(%d,&n); printf(\nenter the elements in ascending order); for(i=0;i<n;i++) { scanf(%d,a[i]); } printf(\nenter the element to be searched); scanf(%d,&key); low=0,high=n-1; while(low<high) { mid=(low+high)/2; if(key==a[mid]) { f=1; break; } else if(key<a[mid]) high=mid-1; else low=mid+1; }
21
OUTPUT:
Enter the limit: 5 Enter the elements in ascending order: 4 7 8 11 21 Enter the element to be searched: 11 The element found.
22
16.
MULTIPLICATION OF A MATRIX
#include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2,i,j,k; clrscr(); printf("\nenter the order of first matrix: "); scanf("%d%d",&m1,&n1); printf("\nenter the order of 2nd matrix:"); scanf("%d%d",&m2,&n2); if(m2==n1) { printf("\nenter the numbers of first matrix:"); for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { scanf("%d",&a[i][j]); } } printf("\nenter the numbers of 2nd matrix: "); for(i=0;i<m2;i++) { for(j=0;j<n2;j++) { scanf("%d",&b[i][j]); } } printf("\nfirst matrix \n");
23
for(i=0;i<m1;i++) { for(j=0;j<n1;j++) { printf("%d\t",a[i][j]); } printf("\n"); } printf("\nsecond matrix \n"); for(i=0;i<m2;i++) { for(j=0;j<n2;j++) { printf("%d\t",b[i][j]); } printf("\n"); } for(i=0;i<m1;i++) { for(j=0;j<n2;j++) { c[i][j]=0; for(k=0;k<m2;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("\nthe resultant matrix \n"); for(i=0;i<m1;i++) {
24
OUTPUT: Enter the order of first matrix: 3 3 Enter the order of 2nd matrix:3 3 Enter the numbers of first matrix:1 0 0 010 020 Enter the numbers of 2nd matrix: 3 0 0 040 105 First matrix 1 0 0 0 1 2 0 0 0
Second matrix 3 0 1 0 4 0 0 0 5
17.
TRANSPOSE OF A MATRIX
#include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],m,n,i,j; clrscr(); printf("\nenter the order of matrix: "); scanf("%d%d",&m,&n); printf("\nenter the numbers of first matrix:"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("\ngiven matrix \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } for(i=0;i<m;i++) { for(j=0;j<n;j++) {
26
b[i][j]=a[j][i] } } printf("\n transpose of given matrix \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d\t",b[i][j]); } printf("\n"); } getch(); }
OUTPUT:
Enter the order the matrix: 3 3 Enter the numbers of first matrix: 120 210 324
Given matrix 1 2 3 2 1 2 0 0 4
18.
#include<stdio.h> #include<conio.h> void add(int a,int b); void sub(int a,int b); void mult(int a,int b); void div(int a,int b); void main() { int a,b; clrscr(); printf(enter the number); scanf(%d%d,&a,&b); printf(addition\n); add(a,b); printf(subtraction\n); sub(a,b); printf(multiplication\n); mult(a,b); printf(division\n); div(a,b); } void add(int x, int y) { int z; z=x+y; printf(sum of %d and %d is %d,x,y,z); } void sub(int x,int y) {
28
int z; z=x-y; printf(difference of %d and %d is %d,x,y,z); } void mult(int x,int y) { int z; z=x*y; printf(product of %d and %d is %d,x,y,z); } void div(int x,int y) { int z; z=x/y; printf(quotient of %d by %d is %d,x,y,z); }
OUTPUT:
Enter the numbers 3 2 Addition Sum of 3 and 2 is 5 Subtraction Difference of 3 and 2 is 1 Multiplication Product of 3 and 2 is 6 Division Quotient of 3 by 2 is 1
29
19.
#include<stdio.h> #include<conio.h> void main() { int n; long int factorial(int n); clrscr(); printf("\n enter the number: "); scanf("%d",&n); printf("n!=%d",factorial(n)); getch(); } long int factorial(int n) { int fact; if(n==1) { return 1; } else fact=n*factorial(n-1); return(fact); }
OUTPUT:
30
20.
FIBONACCI SERIES
#include<stdio.h> #include<conio.h> void main() { int n,f,s,t,i; clrscr(); printf("enter a limit:"); scanf("%d",&n); f=0; s=1; t=f+s; printf("the fibonacci series is:\n%d\t%d\t%d\t",f,s,t); for(i=3;i<n;i++) { f=s; s=t; t=f+s; printf("%d\t",t); } getch(); }
31
21.
#include<stdio.h> #include<conio.h> void main() { char name[10],rev[10]; int i,j=0,count=0,flag=1; clrscr(); printf("enter the string:"); gets(name); for(i=0;name[i]!='\0';i++) { count++; } for(i=count-1;i>=0;i--) { rev[j]=name[i]; j++; } rev[j]='\0'; //puts(rev); if(strcmp(name,rev)==0) { flag=0; } if(flag!=1) { printf("\n\n string is palindrome"); } else { printf(" \n\nthe string is not palindrome"); } getch();}
32
OUTPUT:
33
22.
SORTING OF STRING
#include<stdio.h> #include<conio.h> void main() { int i,j,n; char str[20][20],temp[20]; clrscr(); printf(enter the number of strings:); scanf(enter the strings:); for(i=0;i<n;i++) scanf(%s,str[i]); for(i=0;i<n;i++) { for(j=0;j<n-1,j++) { if(strcmp(str[j],str[j+1])>0) { strcpy(temp,str[j]); strcpy(str[j],str[j+1]); strcpy(str[j+1],temp); }} printf(sorted strings:); for(i=0;i<n;i++) printf(%s\t,str[i]) getch(); }
34
OUTPUT:
35
23.
#include<stdio.h> #include<conio.h> void swap(int *x,int *y); void main() { int x,y; clrscr(); printf(enter two numbers:); scanf(%d%d,&x,&y); printf(before swapping x=%d and y=%d,x,y); swap(&x,&y); printf(after swapping x=%d and y=%d,x,y); getch(); } void swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; }
OUTPUT:
36
24.
#include<stdio.h> #include<conio.h> void main() { struct student { char name[20]; int m1,m2,m3,total; float avg; }s[10]; int n,i; clrscr(); printf("\nenter the number of students"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n\nenter the name of %d student:-",i+1); scanf("%s",s[i].name); printf("\nenter the 3 marks:-"); scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3); s[i].total=s[i].m1+s[i].m2+s[i].m3; s[i].avg=s[i].total/3; printf("\ntotal mark=%d",s[i].total); printf("\n average=%f",s[i].avg); } getch(); }
37
OUTPUT:
enter the number of students3 enter the name of 1 student:-rahul enter the 3 marks:-20 54 78
38
25.
QUADRATIC ROOT
#include<stdio.h> #include<conio.h> #include<math.h> void main () { float a,b,c,x,y,z,d,root1, root2; clrscr(); printf("\nenter the values of a,b and c:"); scanf("%f%f%f",&a,&b,&c); d=(b*b)-(4*a*c); if(d>0) { printf("\nthe roots are real and distinct"); root1=(-b+sqrt(d)/2*a); root2=(-b-sqrt(d)/2*a); printf ("\n\nroot1=%f, \nroot2=%f",root1, root2); } else if (d==0) { printf("\nthe roots are equal"); root1 =(-b)/(2*a); printf("\n\nroot=%f\t %f",root1,root1); } else { printf("\nthe roots are imaginary"); x=-d; y=-b/2*a; z=sqrt(x)/2*a;
39
OUTPUT:
root=-2.000000 -2.000000
root1=-3.000000, root2=-5.000000
40
26.
VOWEL COUNTING
#include<stdio.h> #include<conio.h> #include<string.h> void main() { int v=0,i,l; char s[40]; clrscr(); printf("enter a string:"); gets(s); l=strlen(s); for(i=0;i<l;i++) { if(s[i]=='a'||s[i]=='a'||s[i]=='e'||s[i]=='e'||s[i]=='i'||s[i]=='i'||s[i]=='o'||s[i]=='o'||s[i]=='u'||s[i] =='u') { v++; } } printf("\n number of vowels=%d",v); getch(); }
OUTPUT:
number of vowels=4
41
27.
#include<stdio.h> #include<conio.h> void main() { int a[100],*p[100],i,j,n,t; clrscr(); printf("\nenter the limit:"); scanf("%d",&n); printf("\nenter the numbers:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); p[i]=&a[i]; } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(*p[j]>*p[j+1]) { t=*p[j]; *p[j]=*p[j+1]; *p[j+1]=t; } } } printf("sorted numbers \n"); for(i=0;i<n;i++) {
42
printf("\n%d",*p[i]); } getch(); }
OUTPUT:
43
28.
#include<stdio.h> #include<conio.h> void main() { FILE *fp; int m1,m2,m3,m4,m5,s; char name[20]; float avg; clrscr(); printf("n enter the marks"); scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5); s=m1+m2+m3+m4+m5; avg=s/5; printf("\n Enter the file name:-"); scanf("%s",name); fp=fopen(name,"w"); fprintf(fp,"\nName:-%s",name); fprintf(fp,"\nMarks:-\n%d\t%d\t%d\t%d\t%d",m1,m2,m3,m4,m5); fprintf(fp,"\nTotal Mark=%d",s); fprintf(fp,"\nAverage=%f",avg); fclose(fp); getch(); }
44
OUTPUT
45
29.
#include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char fr1[30],fr2[30],c; clrscr(); printf("enter the source file name"); gets(fr1); printf("enter the destination file name"); gets(fr2); fp1=fopen(fr1,"r"); fp2=fopen(fr2,"w"); if(fp1==NULL) { printf("destination file does not exist"); } while((c=fgetc(fp1))>0) { fputc(c,fp2); } fclose(fp1); fclose(fp2); printf("success"); getch(); }
46
OUTPUT
enter the source file namet1.c enter the destination file name t2.c success
47