Sunteți pe pagina 1din 49

1. PERFECT NUMBER.

void main() { int n,i=1,sum=0; clrscr(); printf(\nEnter a number:-); scanf(%d,&n); while(i<n) { if(n%i==0) sum=sum+i; i++; } if(sum==n) printf(\nThe no %d is a perfect number,i); else printf(\nThe no %d is not a perfect number,i); getch(); } 2. ARMSTRONG NUMBER. void main() { int num,r,sum=0,temp; clrscr(); printf(\nEnter a number:-); scanf(%d,&num); temp=num; while(num!=0) { r=num%10; num=num/10; sum=sum+(r*r*r); } if(sum==temp) printf(\nThe number %d is an armstrong number,temp); else printf(\nThe number %d is not an armstrong number,temp); getch();

3. STRONG NUMBER void main() { int num,i,f,r,sum=0,temp; clrscr(); printf(\nEnter a number); scanf(%d,&num); temp=num; while(num) { i=1,f=1; r=num%10; while(i<=r) { f=f*i; i++; } sum=sum+f; num=num/10; } if(sum==temp) printf(%d is a strong number,temp); else printf(%d is not a strong number,temp); getch(); }

++4. PRIME NUMBER.

void main() { int num,i,count=0; clrscr(); printf(\nEnter a number:); scanf(%d,&num); for(i=1;i<=num;i++) { if(num%i==0) count++; } if(count==2) printf(%d is a prime number,num); else printf(%d is not a prime number,num); getch(); }

5. REVERSE A NUMBER void main() { int num,sum=0,r; clrscr(); printf(\nEnter a number:); scanf(%d,&num); while(num) { r=num%10; sum=sum*10+r; num=num/10; } printf(\nReverse number=%d,sum); getch(); }

6. SUM OF THE DIGITS OF A NUMBER void main() { int num,sum=0,r; clrscr(); printf(\nEnter a number:);

scanf(%d,&num); while(num) { r=num%10; num=num/10; sum=sum+r; } printf(sum=%d,sum); getch(); }

7. PALINDROME NUMBER. void main() { int num,r,sum=0,temp; clrscr(); printf(\nEnter a number:); scanf(%d,&num); temp=num; while(num) { r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf(\n%d is a palindrome,temp); else printf(\n%d is not a palindrome,temp); getch(); }

8. G.C.D OF TWO NUMBERS void main() { int n1,n2; clrscr(); printf(\nEnter two numbers:); scanf(%d %d,&n1,&n2); while(n1!=n2) { if(n1>n2) n1=n1-n2; else n2=n2-n1; } printf(\nGCD=%d,n1); getch(); }

9. L.C.M OF TWO NUMBERS. void main() { int n1,n2,x,y; clrscr(); printf(\nEnter two numbers:); scanf(%d %d,&n1,&n2); x=n1,y=n2; while(n1!=n2) { if(n1>n2) n1=n1-n2;

else n2=n2-n1; } printf(L.C.M=%d,x*y/n1); getch(); }

10. SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE void main() { int a,b; clrscr(); printf(\nEnter two numbers:); scanf(%d %d,&a,&b); printf(\nBefore swapping a=%d b=%d,a,b); a=a^b; b=b^a; a=a^b; printf(\nAfter swapping a=%d b=%d,a,b); getch(); }

11. FLOYDS TRIANGLE 1 23 456 void main() { int i,j,r,k=1; clrscr(); printf(\nEnter the range:); scanf(%d,&r);

printf(\nFLOYDS TRIANGLE\n\n); for(i=1;i<=r;i++) { for(j=1;j<=i;j++,k++) printf( %d,k); printf(\n); } getch(); } 12. PRIME FACTORS OF A NUMBER void main() { int num,i=1,j,k; clrscr(); printf(\nEnter a number:); scanf(%d,&num); while(i<=num) { k=0; if(num%i==0) { j=1; while(j<=i) { if(i%j==0) k++; j++; } if(k==2) printf(\n%d is a prime factor,i); } i++; } getch(); } 13. MULTIPLICATION TABLE void main() { int r,i,j,k; clrscr(); printf(\nEnter the number range:-); scanf(%d,&r); for(i=1;i<=r;i++) { for(j=1;j<=10;j++) printf( %d*%d=%d,i,j,i*j);

printf(\n); } getch(); } 14. FACTORIAL OF A NUMBER void main() { int i=1,f=1,num; clrscr(); printf(\nEnter a number:); scanf(%d,&num); while(i<=num) { f=f*i; i++; } printf(\nFactorial of %d is:%d,num,f); getch(); } 15. FIBONACCI SERIES void main() { int i=0,j=1,k=2,r,f; clrscr(); printf(Enter the number range:); scanf(%d,&r); printf(\nFIBONACCI SERIES: ); printf(%d %d,i,j); while(k<r) { f=i+j; i=j; j=f; printf( %d,j); k++; } getch(); }

16. PRINTING ASCII VALUE

void main() { int i; clrscr(); for(i=0;i<=255;i++) { printf(%d -> %c ,i,i); delay(10); } getch(); } 17. CHECKING LEAP YEAR void main() { int year; clrscr(); 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); getch(); } 18. CONVERSION OF DECIMAL TO BINARY void main() { int n,m,no=0,a=1,rem; clrscr(); printf(Enter any decimal number->); scanf(%d,&n); m=n; while(n!=0) { rem=n%2; no=no+rem*a; n=n/2; a=a*10; } printf(The value %d in binary is->,m); printf(%d,no); getch(); }

19. CONVERSION OF BINARY TO DECIMAL void main() { long int no,n=0,j=1,rem,no1; clrscr(); printf(Enter any number any binary form->); scanf(%ld,&no); no1=no; while(no!=0) { rem=no%10; n=n+rem*j; j=j*2; no=no/10; } printf(\nThe value of binary no. %ld is ->%ld,no1,n); getch(); }

20. SWAPING OF TWO ARRAYS void main() { int a[10],b[10],c[10],i; clrscr(); printf(Enter First array->); for(i=0;i<10;i++) scanf(%d,&a[i]); printf(\nEnter Second array->); for(i=0;i<10;i++) scanf(%d,&b[i]); printf(Arrays before swapping); printf(\nFirst array->); for(i=0;i<10;i++) { printf(%d,a[i]); } printf(\nSecond array->); for(i=0;i<10;i++) { printf(%d,b[i]); } for(i=0;i<10;i++) { //write any swapping technique c[i]=a[i]; a[i]=b[i]; b[i]=c[i]; } printf(\nArrays after swapping); printf(\nFirst array->); for(i=0;i<10;i++) { printf(%d,a[i]); } printf(\nSecond array->); for(i=0;i<10;i++) { printf(%d,b[i]); } getch(); }

21. FINDING NCR FACTOR void main() { int n,r,ncr; clrscr(); printf(Enter any two numbers->); scanf(%d %d,&n,&r); ncr=fact(n)/(fact(r)*fact(n-r)); printf(The NCR factor of %d and %d is %d,n,r,ncr); getch(); } int fact(int n) { int i=1; while(n!=0) { i=i*n; n; } return i; }

22. PASCALS TRIANGLE void main() { int line,i,j,k; clrscr(); printf(Enter the no. of lines); scanf(%d,&line); for(i=1;i<=line;i++) { for(j=1;j<=line-i;j++) printf( ); for(k=1;k<i;k++) printf(%d,k); for(k=i;k>=1;k) printf(%d,k); printf(\n);

} getch(); }

23. CONVERSION FROM UPPERCASE TO LOWER CASE void main() { char str[20]; int i; clrscr(); printf(Enter any string->); scanf(%s,str); printf(The string is->%s,str); for(i=0;i<=strlen(str);i++) { if(str[i]>=65&&str[i]<=90) str[i]=str[i]+32; } printf(\nThe string in uppercase is->%s,str); getch(); }

24. CONVERSION FROM LOWER CASE TO UPPER CASE void main() { char str[20]; int i; clrscr(); printf(Enter any string->); scanf(%s,str); printf(The string is->%s,str); for(i=0;i<=strlen(str);i++)

{ if(str[i]>=97&&str[i]<=122) str[i]=str[i]-32; } printf(\nThe string in lowercase is->%s,str); getch(); }

25. DELETE THE VOWELS FROM A STRING void main() { char str[20],s[20]; int i,j=0; clrscr(); printf(Enter any string->); scanf(%s,str); printf(The string is->%s,str); for(i=0;i<=strlen(str);i++) { if(str[i]==a'||str[i]==e'||str[i]==i'||str[i]==o'||str[i]==u') str[i]= ; else s[j++]=str[i]; } s[j]=; printf(\nThe string without vowel is->%s,s); getch(); }

26. ADDITION OF MATRICES void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf(Enter the First matrix->); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(%d,&a[i][j]); printf(\nEnter the Second matrix->); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf(%d,&b[i][j]); printf(\nThe First matrix is\n); for(i=0;i<3;i++) { printf(\n); for(j=0;j<3;j++) printf(%d\t,a[i][j]); } printf(\nThe Second matrix is\n); for(i=0;i<3;i++)

{ printf(\n); for(j=0;j<3;j++) printf(%d\t,b[i][j]); } for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf(\nThe Addition of two matrix is\n); for(i=0;i<3;i++) { printf(\n); for(j=0;j<3;j++) printf(%d\t,c[i][j]); } getch(); }

27. STRING PALINDROME #includestring.h void main() { char *str,*rev; int i,j; clrscr(); printf(\nEnter a string:); scanf(%s,str); for(i=strlen(str)-1,j=0;i>=0;i,j++) rev[j]=str[i]; rev[j]=; if(strcmp(rev,str)) printf(\nThe string is not a palindrome); else printf(\nThe string is a palindrome); getch(); } 28. COPY DATA FROM ONE FILE TO ANOTHER FILE #includestdio.h void main()

{ FILE *p,*q; char file1[20],file2[20]; char ch; clrscr(); printf(\nEnter the source file name to be copied:); gets(file1); p=fopen(file1,r); if(p==NULL) { printf(cannot open %s,file1); exit(0); } printf(\nEnter the destination file name:); gets(file2); q=fopen(file2,w); if(q==NULL) { printf(cannot open %s,file2); exit(0); } while((ch=getc(p))!=EOF) putc(ch,q); printf(\nCOMPLETED); fclose(p); fclose(q); getch(); }

29. ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS void main() { int a,b,c,d,x,y; clrscr(); printf(\nEnter the first complex number:); scanf(%d%d,&a,&b); printf(\nEnter the second complex number:); scanf(%d%d,&c,&d); if(b<0)

printf(%d-i\n,a-b); else printf(d+i\n,a+b); if(d<0) printf(d-i\n,c-d); else printf(%d+i\n,c+d); printf(\nADDITION ); x=a+c; y=b+d; if(y>0) printf(%d-i%d,x,-y); else printf(%d+i%d,x,+y); printf(\n\nSUBTRACTION ); x=a-c; y=b-d; if(y<0) printf(%d-i%d,x,-y); else printf(%d+i%d,x,+y); getch(); }

30. SUM OF THE SERIES 1+2+3++n void main() { int r; clrscr(); printf(\nEnter the number range: ); scanf(%d,&r); printf(\nSum of the series is: %d,(r*(r+1))/2);

getch(); }

31. SUM OF SQUARES OF THE SERIES 12+22+32++n2 void main() { long int r; clrscr(); printf(\nEnter the range: ); scanf(%ld,&r); printf(\nSum of the squares of the series is: %ld,((r*(r+1))*(2*r+1))/6); getch(); }

32. SUM OF CUBES OF THE SERIES 13+23+33++n3 void main() { int r; clrscr(); printf(\nEnter the number range: ); scanf(%d,&r); printf(\nSum of the cubes of the series is: %d,(r*(r+1)/2)*(r*(r+1)/2)); getch(); }

33. LARGEST NUMBER IN AN ARRAY void main() { int a[50],size,i,big; clrscr();

printf(\nEnter the size of the array: ); scanf(%d,&size); printf(\nEnter %d elements in to the array: , size); for(i=0;i<size;i++) scanf(%d,&a[i]); big=a[0]; for(i=1;i<size;i++) { if(big<a[i]) big=a[i]; } printf(\nBiggest: %d,big); getch(); }

34. SECOND LARGEST NUMBER IN AN UNSORTED ARRAY main() { int un[10], i, big1, big2; printf(Enter array elements: ); for ( i = 0; i < 10; ++i ) scanf(%d, &un[i]); big1 = un[0]; for ( i = 1; i < 10; ++i ) { if ( big1 < un[i] ) big1 = un[i]; if ( big1 != un[0] ) big2 = un[0]; else big2 = un[1]; } for ( i = 1; i < 10; ++i ) { if ( big1 != un[i] && big2 < un[i] ) big2 = un[i]; } printf(Second largest: %d\n, big2); return 0; }

35. SECOND SMALLEST NUMBER IN AN UNSORTED ARRAY main() { int un[10], i, s1, s2; clrscr(); printf(Enter array elements: ); for ( i = 0; i < 10; ++i ) scanf(%d, &un[i]); s1 = un[0]; for ( i = 1; i < 10; ++i ) { if ( s1 > un[i] ) s1 = un[i]; if ( s1 != un[0] ) s2 = un[0]; else s2 = un[1]; } for ( i = 1; i < 10; ++i ) { if ( s1 != un[i] && s2 > un[i] ) s2 = un[i]; } printf(\nSecond smallest: %d, s2); return 0; }

36. MULTIPLICATION OF MATRICES

void main() { int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p; clrscr(); printf(\nEnter the row and column of first matrix); scanf(%d %d,&m,&n); printf(\nEnter the row and column of second matrix); scanf(%d %d,&o,&p); if(n!=o) { printf(Matrix mutiplication is not possible); printf(\nColumn of first matrix must be same as row of second matrix); } else { printf(\nEnter the First matrix->); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(%d,&a[i][j]); printf(\nEnter the Second matrix->); for(i=0;i<o;i++) for(j=0;j<p;j++) scanf(%d,&b[i][j]); printf(\nThe First matrix is\n); for(i=0;i<m;i++) { printf(\n); for(j=0;j<n;j++) { printf(%d\t,a[i][j]); } } printf(\nThe Second matrix is\n); for(i=0;i<o;i++) { printf(\n); for(j=0;j<p;j++) { printf(%d\t,b[i][j]); }} for(i=0;i<m;i++) for(j=0;j<p;j++) c[i][j]=0; for(i=0;i<m;i++)//row of first matrix { for(j=0;j<p;j++)//column of second matrix { sum=0; for(k=0;k<n;k++) sum=sum+a[i][k]*b[k][j];

c[i][j]=sum; } } } printf(\nThe multiplication of two matrix is\n); for(i=0;i<m;i++) { printf(\n); for(j=0;j<p;j++) { printf(%d\t,c[i][j]); } } getch(); }

37. SUM OF DIAGONAL ELEMENTS OF A MATRIX void main() { int a[10][10],i,j,sum=0,m,n; clrscr(); printf(\nEnter the row and column of matrix); scanf(%d %d,&m,&n); printf(\nEnter the First matrix->); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(%d,&a[i][j]); printf(\nThe matrix is\n); for(i=0;i<m;i++) { printf(\n); for(j=0;j<m;j++) { printf(%d\t,a[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(i==j) sum=sum+a[i][j]; } } printf(\n\nSum of the diagonal elements of a matrix is -> );

printf(%d,sum); getch(); }

38. TRASPOSE OF A MATRIX void main() { int a[10][10],b[10][10],i,j,k=0,m,n; clrscr(); printf(\nEnter the row and column of matrix); scanf(%d %d,&m,&n); printf(\nEnter the First matrix->); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf(%d,&a[i][j]); printf(\nThe matrix is\n); for(i=0;i<m;i++) { printf(\n); for(j=0;j<m;j++) { printf(%d\t,a[i][j]); } } for(i=0;i<m;i++) for(j=0;j<n;j++) b[i][j]=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { b[i][j]=a[j][i]; printf(\n%d,b[i][j]); } } printf(\n\nTraspose of a matrix is -> ); for(i=0;i<m;i++) { printf(\n); for(j=0;j<m;j++) {

printf(%d\t,b[i][j]); } } getch(); }

39. CONVERSION OF FAREHNITE TO CENTIGRADE void main() { float c,f; clrscr(); printf(Enter temp. in farehnite); scanf(%f,&f); c=(5*(f-32))/9;//Formula for conversion printf(The temp. in centigrade is->%f,c); getch(); } 40. COUNTING DIFFERENT CHARACTERS IN A STRING main() { int a[26],A[26],i,c=0; char str[100]; clrscr(); puts(Enter a string->); gets(str); for(i=0;i<26;i++) { a[i]=0; A[i]=0; } for(i=0;str[i]!=;i++) { c=str[i]; if(c<97) { c=c-65; A[c]++; } else { c=c-97; a[c]++; }

} for(i=0;i<26;i++) { if(a[i]!=0) printf(\n%c occurs %d times,i+97,a[i]); } for(i=0;i<26;i++) { if(A[i]!=0) printf(\n%c occurs %d times,i+97,A[i]); } getch(); } 41. SORTING OF STRING void main() { int i,j,n; char str[20][20],temp[20]; clrscr(); puts(Enter the no. of string to be sorted); scanf(%d,&n); for(i=0;i<=n;i++) gets(str[i]); for(i=0;i<=n;i++) for(j=i+1;j<=n;j++) { if(strcmp(str[i],str[j])>0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } printf(The sorted string\n); for(i=0;i<=n;i++) puts(str[i]); getch(); } 42. BUBBLE SORT void main() { int s,temp,i,j,a[20]; clrscr(); printf(\nEnter size of the array: ); scanf(%d,&s); printf(\nEnter %d elements in to the array:,s);

for(i=0;i<s;i++) scanf(%d,&a[i]); for(i=0;i<s-1;i++) { for(j=0;j<s-1-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf(\nThe array after sorting is: ); for(i=0;i<s;i++) printf( %d,a[i]); getch(); } 43. SELECTION SORT void main() { int s,i,j,temp,a[20]; clrscr(); printf(\nEnter size of the array :); scanf(%d,&s); printf(\nEnter %d elements in to the array:); for(i=0;i<s;i++) scanf(%d,&a[i]); for(i=0;i<s;i++) { for(j=i+1;j<s;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf(\nThe array after sorting is: ); for(i=0;i<s;i++) printf( %d,a[i]); getch(); } 44. INSERTION SORT

void main() { int i,j,s,temp,a[20]; clrscr(); printf(\nEnter size of the array: ); scanf(%d,&s); printf(\nEnter %d elements in to the array:,s); for(i=0;i<s;i++) scanf(%d,&a[i]); for(i=1;i<s;i++) { temp=a[i]; j=i-1; while((temp<a[j])&&(j>=0)) { a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } printf(\nAfter sorting the elements are: ); for(i=0;i<s;i++) printf( %d,a[i]); getch(); } 45. DISPLAY SOURCE CODE AS OUTPUT #includestdio.h void main() { FILE *p; char ch; clrscr(); p=fopen(raja.c,r); while((ch=getc(p))!=-1) putchar(ch); fclose(p); getch(); } 46. FACTORIAL OF A NUMBER USING RECURSION void main() { int num,f; clrscr(); printf(\nEnter a number: ); scanf(%d,&num);

f=fact(num); printf(\nFactorial of %d is: %d,num,f); getch(); } int fact(int n) { if(n==1) return 1; else return(n*fact(n-1)); } 47. GCD OF A NUMBER USING RECURSION void main() { int n1,n2,gcd; clrscr(); printf(\nEnter two numbers: ); scanf(%d %d,&n1,&n2); gcd=findgcd(n1,n2); printf(\nGCD of %d and %d is: %d,n1,n2,gcd); getch(); } int findgcd(int x,int y) { while(x!=y) { if(x>y) return findgcd(x-y,y); else return findgcd(x,y-x); } return x; } 48. SUM OF DIGITS OF A NUMBER USING RECURSION void main() { int num,x; clrscr(); printf(\nEnter a number: ); scanf(%d,&num); x=findsum(num); printf(Sum of the digits of %d is: %d,num,x); getch(); } int r,s; int findsum(int n)

{ if(n) { r=n%10; s=s+r; findsum(n/10); } else return s; } 49. POWER OF A NUMBER void main() { int pow,num,i=1; long int sum=1; clrscr(); printf(\nEnter a number: ); scanf(%d,&num); printf(\nEnter power: ); scanf(%d,&pow); while(i<=pow) { sum=sum*num; i++; } printf(\n%d to the power %d is: %ld,num,pow,sum); getch(); }

50. POWER OF A NUMBER USING RECURSION void main() { int pow,num; long int res; long int power(int,int); clrscr(); printf(\nEnter a number: ); scanf(%d,&num); printf(\nEnter power: ); scanf(%d,&pow); res=power(num,pow); printf(\n%d to the power %d is: %ld,num,pow,res); getch();

} int i=1; long int sum=1; long int power(int num,int pow) { if(i<=pow) { sum=sum*num; power(num,pow-1); } else return sum; } 51. SWAPPING OF STRINGS void main() { int i=0,j=0,k=0; char str1[20],str2[20],temp[20]; clrscr(); puts(Enter first string); gets(str1); puts(Enter second string); gets(str2); printf(Before swaping the strings are\n); puts(str1); puts(str2); while(str1[i]!=) { temp[j++]=str1[i++]; } temp[j]=; i=0,j=0; while(str2[i]!=) { str1[j++]=str2[i++]; } str1[j]=; i=0,j=0; while(temp[i]!=) { str2[j++]=temp[i++]; } str2[j]=; printf(After swaping the strings are\n); puts(str1); puts(str2); getch(); }

52. CONCATENATION OF TWO STRINGS void main() { int i=0,j=0; char str1[20],str2[20]; clrscr(); puts(Enter first string); gets(str1); puts(Enter second string); gets(str2); printf(Before concatenation the strings are\n); puts(str1); puts(str2); while(str1[i]!=) { i++; } while(str2[j]!=) { str1[i++]=str2[j++]; } str1[i]=; printf(After concatenation the strings are\n); puts(str1); getch(); }

53. CONCATENATION OF TWO STRINGS USING POINTER void main() { int i=0,j=0; char *str1,*str2,*str3; clrscr(); puts(Enter first string); gets(str1); puts(Enter second string);

gets(str2); printf(Before concatenation the strings are\n); puts(str1); puts(str2); while(*str1) { str3[i++]=*str1++; } while(*str2) { str3[i++]=*str2++; } str3[i]=; printf(After concatenation the strings are\n); puts(str3); getch(); } 54. ACROMATIC STRING(PRINTING INITIALS) void main() { char str[20]; int i=0; clrscr(); puts(Enter a string); gets(str); printf(%c,*str); while(str[i]!=) { if(str[i]== ) { i++; printf(%c,*(str+i)); } i++; } getch(); }

55. LINEAR SEARCH void main() {

int a[10],i,n,m,c=0; clrscr(); printf(Enter the size of an array->); scanf(%d,&n); printf(\nEnter the elements of the array->); for(i=0;i<n;i++) { scanf(%d,&a[i]); } printf(\nThe elements of an array are->); for(i=0;i<n;i++) { printf( %d,a[i]); } printf(\nEnter the number to be search->); scanf(%d,&m); for(i=0;i<n;i++) { if(a[i]==m) { c=1; break; } } if(c==0) printf(\nThe number is not in the list); else printf(\nThe number is found); getch(); } 56. BINARY SEARCH void main() { int a[10],i,n,m,c=0,l,u,mid; clrscr(); printf(Enter the size of an array->); scanf(%d,&n); printf(\nEnter the elements of the array->); for(i=0;i<n;i++) { scanf(%d,&a[i]); } printf(\nThe elements of an array are->); for(i=0;i<n;i++) { printf( %d,a[i]); } printf(\nEnter the number to be search->);

scanf(%d,&m); l=0,u=n-1; while(l<=u) { mid=(l+u)/2; if(m==a[mid]) { c=1; break; } else if(m<a[mid]) { u=mid-1; } else l=mid+1; } if(c==0) printf(\nThe number is not in the list); else printf(\nThe number is found); getch(); } 57. BINARY SEARCH THROUGH RECURSSION void main() { int a[10],i,n,m,c,l,u; clrscr(); printf(Enter the size of an array->); scanf(%d,&n); printf(\nEnter the elements of the array->); for(i=0;i<n;i++) { scanf(%d,&a[i]); } printf(\nThe elements of an array are->); for(i=0;i<n;i++) { printf( %d,a[i]); } printf(\nEnter the number to be search->); scanf(%d,&m); l=0,u=n-1; c=binary(a,n,m,l,u); if(c==0) printf(\nThe number is not in the list); else

printf(\nThe number is found); getch(); } int binary(int a[],int n,int m,int l,int u) { int mid,c=0; if(l<=u) { mid=(l+u)/2; if(m==a[mid]) { c=1; } else if(m<a[mid]) { return binary(a,n,m,l,mid-1); } else return binary(a,n,m,mid+1,u); } else return c; } 58. REMOVE DUPLICATE ELEMENTS IN AN ARRAY void main() { int arr[50]; int *p; int i,j,k,size,n; clrscr(); printf(\nEnter size of the array: ); scanf(%d,&n); printf(\nEnter %d elements into the array: ,n); for(i=0;i<n;i++) scanf(%d,&arr[i]); size=n; p=arr; for(i=0;i<size;i++) { for(j=0;j<size;j++) { if(i==j) { continue; } else if(*(p+i)==*(p+j)) {

k=j; size; while(k < size) { *(p+k)=*(p+k+1); k++; } j=0; } } } printf(\nThe array after removing duplicates is: ); for(i=0;i < size;i++) { printf( %d,arr[i]); } getch(); } 59. REVERSE A NUMBER USING RECURSION void main() { int num,rev; clrscr(); printf(\nEnter a number :); scanf(%d,&num); rev=reverse(num); printf(\nAfter reverse the no is :%d,rev); getch(); } int sum=0,r; reverse(int num) { if(num) { r=num%10; sum=sum*10+r; reverse(num/10); } else return sum; } 60. CONVERSION FROM DECIMAL TO OCTAL void main() { int i=0,j=0,rem=0,a[10],b[10]; long int num;

clrscr(); printf(\nEnter a number :); scanf(%ld,&num); while(num) { if(num<8) { a[j++]=num; break; } else { a[j++]=num%8; num=num/8; } } for(i=j-1;i>=0;i) b[rem++]=a[i]; printf(\nOctal equivalent :); for(j=0;j<rem;j++) printf(%d,b[j]); getch(); } 61. QUICK SORT void main() { int x[20],size,i; clrscr(); printf(\nEnter size of the array :); scanf(%d,&size); printf(\nEnter %d elements :,size); for(i=0;i<size;i++) scanf(%d,&x[i]); quicksort(x,0,size-1); printf(\nSorted elements :); for(i=0;i<size;i++) printf( %d,x[i]); getch(); } quicksort(int x[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first; i=first; j=last; while(i<j)

{ while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } } 62. DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION void main() { int a[50],i,pos,size; clrscr(); printf(\nEnter size of the array: ); scanf(%d,&size); printf(\nEnter %d elements in to the array: ,size); for(i=0;i<size;i++) scanf(%d,&a[i]); printf(\nEnter position where to delete: ); scanf(%d,&pos); i=0; while(i!=pos-1) i++; while(i<10) { a[i]=a[i+1]; i++; } size; for(i=0;i<size;i++) printf( %d,a[i]); getch(); } 63. INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION

void main() { int a[50],size,num,i,pos,temp; clrscr(); printf(\nEnter size of the array: ); scanf(%d,&size); printf(\nEnter %d elements in to the array: ,size); for(i=0;i<size;i++) scanf(%d,&a[i]); printf(\nEnter position and number to insert: ); scanf(%d %d,&pos,&num); i=0; while(i!=pos-1) i++; temp=size++; while(i<temp) { a[temp]=a[temp-1]; temp; } a[i]=num; for(i=0;i<size;i++) printf( %d,a[i]); getch(); } 64. GREATEST AMONG 3 NUMBERS USING BINARY MINUS void main() { int a,b,c; clrscr(); printf(\nEnter 3 numbers: ); scanf(%d %d %d,&a,&b,&c); if(a-b>0 && a-c>0) printf(\nGreatest is a :%d,a); else if(b-c>0) printf(\nGreatest is b :%d,b); else printf(\nGreatest is c :%d,c); getch(); } 65. A 5 DIGIT NUMBER IS INPUT THROUGH THE KEY BOARD.THE OUTPUT IS A NEW NUMBER ADDING 1 TO EACH OF ITS DIGITS. [EXAMPLE: Input-12395 Output-23406]

void main() { long int num; int add(long int); clrscr(); printf(\nEnter a 5 digit number: ); scanf(%ld,&num); add(num); getch(); } add(long int num) { long int r; if(num) { r=num%10; r=r+1; if(r==10) r=0; add(num/10); printf(%d,r); } else return; } 66. GREATEST AMONG 3 NUMBERS USING CONDITIONAL OPERATOR void main() { int a,b,c,big; clrscr(); printf(\nEnter 3 numbers:); scanf(%d %d %d,&a,&b,&c); big=(a>b&&a>c?a:b>c?b:c); printf(\nThe biggest number is:%d,big); getch(); } 67. PROGRAM TO FIND GENERIC ROOT OF A NUMBER. Eg: [34567=3+4+5+6+7=25=2+5=7] void main() { long int num,sum,r; clrscr(); printf(\nEnter a number:-); scanf(%ld,&num); while(num>10)

{ sum=0; while(num) { r=num%10; num=num/10; sum+=r; } if(sum>10) num=sum; else break; } printf(\nSum of the digits in single digit is: %ld,sum); getch(); } 68. PRINT GIVEN STRING FROM FIRST OCCURRENCE OF GIVEN CHARACTER #includestring.h #includestdio.h void main() { char *p; char s[20],s1[1]; clrscr(); printf(\nEnter a string: ); scanf(%[^\n],s); fflush(stdin); printf(\nEnter character: ); gets(s1); p=strpbrk(s,s1); printf(\nThe string from the given character is: %s,p); getch(); } 69. COUNT THE NUMBER OF OCCURRENCES OF ANY TWO VOWELS IN SUCCESSION IN A LINE OF TEXT. #include stdio.h int isvowel(char chk); int main() { char text[1000], chk; int count; count = 0; while( (text[count] = getchar()) != \n ) count++; text[count] = ; count = 0;

while ( (chk = text[count]) != ) { if ( isvowel(chk) ) { if ( (chk = text[++count]) && isvowel(chk) ) { putchar(text[count -1]); putchar(text[count]); putchar(\n); } } else ++count; } return 0; } int isvowel(char chk) { if ( chk == a || chk == e || chk == i || chk == o || chk == u ) return 1; return 0; } 70. PRINT PRIME NUMBERS BETWEEN 1-300 USING BREAK AND CONTINUE #include math.h #include stdio.h main() { int i, j; i = 1; while ( i < 300 ) { j = 2; while ( j < sqrt(i) ) { if ( i % j == 0 ) break; else { ++j; continue; } } if ( j > sqrt(i) ) printf(%d\t, i); ++i; } return 0; }

71. CALCULATE AREA OF A CIRCLE #include stdio.h #define PI 3.141 int main() { float r, a; printf(Radius: ); scanf(%f, &r); a = PI * r * r; printf(%f\n, a); return 0; } 72. AMICABLE PAIRS [Two numbers are said to be amicable if sum of proper divisors of 1st is equal to the 2nd and vice versa. It is assumed that these two numbers shoud be distinct, e.g. 3 is equal to the sum of its proper divisors 1 and 2 but can not form an amicable pair.] #include stdio.h int main() { long int range, test, chk, div, sum, n1, n2; printf(Input range to check amicable numbers: ); scanf(%ld, &range); test = 0; while ( ++test < range ) { sum = div = 0; while ( ++div <= test/2 ) { if ( test % div == 0 ) sum += div; } chk = sum; sum = div = 0; while ( ++div <= chk/2 ) { if ( chk % div == 0 ) sum += div; } if ( sum == test ) { if ( test == chk ) continue; n1 = test; if ( n1 == n2) continue; n2 = chk; printf(%d\t%d\n, n1, n2); }

} return 0; } 73. PASSING ONE-DIMENSIONAL ARRAY TO A FUNCTION #include stdio.h #define N 5 void fstore1D(int a[], int a_size); void fretrieve1D(int a[], int a_size); void fedit1D(int a[], int a_size); int main() { int a[N]; printf(Input data into the matrix:\n); fstore1D(a, N); fretrieve1D(a, N); fedit1D(a, N); fretrieve1D(a, N); return 0; } void fstore1D(int a[], int n) { int i; for ( i = 0; i < n; ++i ) scanf(%d, &a[i]); } void fretrieve1D(int a[], int n) { int i; for ( i = 0; i < n; ++i ) printf(%6d , a[i]); printf(\n); } void fedit1D(int a[], int n) { int i, q; for ( i = 0; i < n; ++i ) { printf(Prev. data: %d\nEnter 1 to edit 0 to skip., a[i]); scanf(%d, &q); if ( q == 1 ) { printf(Enter new value: ); scanf(%d, &a[i]); } } } 74. PASSING 2-DIMENSIONAL ARRAY TO A FUNCTION

#include stdio.h #define M 3 #define N 5 void fstore2D(int a[][N]); void fretrieve2D(int a[][N]); int main() { int a[M][N]; printf(Input data in matrix (%d X %d)\n, M, N); fstore2D(a); fretrieve2D(a); return 0; } void fstore2D(int a[][N]) { int i, j; for ( i = 0; i < M; ++i ) { for ( j = 0; j < N; ++j ) scanf(%d, &a[i][j]); } } void fretrieve2D(int a[][N]) { int i, j; for ( i = 0; i < M; ++i ) { for ( j = 0; j < N; ++j ) printf(%6d , a[i][j]); printf(\n); } }

75. DRAW THE FOLLOWING PYRAMID 1 01 101 0101 void main()

{ int i,j; clrscr(); for(i=1;i<10;i++) { for(j=i;j>=1;j) { printf(%d,j%2); } printf(\n); } getch(); } 76. CREATE A FILE AND STORE DATA IN IT #includestdio.h void main() { FILE *fp; char ch; clrscr(); fp=fopen(file.txt,w); printf(\nEnter data to be stored in to the file:); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); getch(); } 77. WRITING OF STRINGS TO A FILE #includestdio.h void main() { FILE *p; char str[70]; if((p=fopen(string.txt,w))==NULL) { printf(\nUnable to open file string.txt); exit(1); } else printf(\nEnter a set of strings,Press just enter key to finish\n: ); while(strlen(gets(str))>0) { fputs(str,p); fputs(\n,p); } fclose(p);

getch(); } 78. READING OF STRINGS FROM A FILE #includestdio.h void main() { char str[70]; FILE *p; clrscr(); if((p=fopen(string.txt,r))==NULL) { printf(\nUnable t open file string.txt); exit(1); } while(fgets(str,70,p)!=NULL) puts(str); fclose(p); } 79. WRITING OF ENTIRE ARRAY TO A FILE #includestdio.h void main() { FILE *p; int i,a[10]; if((p=fopen(myfile.dat,wb))==NULL) { printf(\nUnable to open file myfile.dat); exit(1); } printf(\nEnter ten values, one value on each line\n); for(i=0;i<10;i++) scanf(%d,&a[i]); fwrite(a,sizeof(a),1,p); fclose(p); getch(); }

80. CONCATENATE MANY FILES AND STORE THEM IN A FILE NAMED FILES. [Use command line arguments to pass the file names] #includestdio.h void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc); void main(int argc,char *argv[]) { FILE *fp1,*fp2; concatenate(fp1,fp2,argv,argc); getch(); } void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc) { int i,ch; fp2=fopen(files,a); for(i=1;i<argc-1;i++) { fp1=fopen(argv[i],r); while((ch=getc(fp1))!=EOF) putc(ch,fp2); } } 81. COUNT NO OF OCCURRENCES OF A CHARACTER IN A STRING USING getchar() AND putchar(). #includestdio.h main() { char name[20]; int i,count=0; clrscr(); for(i=0;i<20;i++) { name[i]=getchar(); putchar(name[i]); if(name[i]==\n) break; else if(name[i]==a') count++; } printf(\nThe number of as in name :%d,count); getch(); return 0; }
ii

Deepak.rick@gmail.com Deepak.rick@yahoo.co.in

S-ar putea să vă placă și