You are on page 1of 12

/* sum of the digits of a number*/ #include<stdio.h> #include<conio.

h> void main() { int a,n,sum=0; clrscr(); printf("Enter the number"); scanf("%d",&a); do { n=a%10; sum=sum+n; a=a/10; } while(a!=0); printf("The sum of digits is %d",sum); getch(); } /*Reverse the digits of a number */ #include<stdio.h> #include<conio.h> int rev(int number); void main() { int p,q; clrscr(); printf("Enter the number:"); scanf("%d",&p); q=rev(p); printf("Reverse of %d is %d \n",p,q); getch(); } int rev(int n) { int digit,revof=0; while(n!=0) { digit=n%10; revof=revof*10+digit; n=n/10; } return(revof); }

/*combinations of a 4-digit number */ #include<stdio.h> #include<conio.h> void main() { int i,j,k,l,m; clrscr();

for(i=0;i<4;i++) { for(j=i+1;j<=4;j++) { printf("%d",j); } for(k=1;k<=i;k++) { printf("%d",k); } printf("\n"); for(k=i;k>=1;k--) { printf("%d",k); } for(j=4;j>=i+1;j--) { printf("%d",j); } getch(); } /*largest digit of a number */ #include<stdio.h> #include<conio.h> void main() { int n,max=0,rem; clrscr(); printf("\n enter a number:"); scanf("%d",&n); while(n!=0) { rem=n%10; n=n/10; if(rem>max) { max=rem; }} printf("\n the largest digit is: %d",max); getch(); } /* number and its reverse are same or not */ #include<stdio.h> #include<conio.h> void main() { int a,num,rem,sum=0; clrscr(); printf("Enter the number:"); scanf("%d",&num); a=num; while(num!=0) { rem=num%10;

num=num/10; sum=sum*10+rem; } if(a==sum) printf("\n the reverse number is same"); else printf("\n the reverse number is not same"); getch(); } /*Armstrong number or not */ #include<stdio.h> #include<conio.h> void main() { int a,b,c,d=0,e; clrscr(); printf("Enter the number:"); scanf("%d",&a); e=a; while(a>0) { b=a%10; c=b*b*b; a=a/10; d=c+d; } if(e==d) printf("%d is an armstrong number",e); else printf("%d is not armstrong number",e); getch(); } /* sum of odd-positioned digits and even-positioned digits of a number */ #include <stdio.h> #include<conio.h> void main() { int oddSum = 0, evenSum = 0, i = 0; char n[50] = {0}; clrscr(); printf("Enter the number "); scanf("%s", n); while(n[i] != '\0'){ if(i % 2 == 0) oddSum = oddSum + (n[i] - 48); else evenSum = evenSum + (n[i] - 48); ++i; } printf("Odd sum is %d and even sum is %d\n", oddSum, evenSum); getch(); } /*second highest number from a set of numbers */

#include<stdio.h> #include<conio.h> void main() { int n,temp,a[20],i,j; clrscr(); printf("\n Enter the number of elements\n"); scanf("%d",&n); printf("Enter the elements\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("The 2nd largest element in the given array is %d",a[n-2]); getch(); } /*Fibonacci series*/ #include<stdio.h> #include<conio.h> void main() { int k,r; int i=0,j=1,f; clrscr(); printf("Enter the number range:"); scanf("%d",&r); printf("\nFIBONACCI SERIES: "); printf("%d %d",i,j); for(k=2;k<r;k++) { f=i+j; i=j; j=f; printf(" %d",j); } getch(); } /*numbers between 1 and 100 which are divisible by 2 and not divisible by 3 and 5 */ #include<stdio.h>

#include<conio.h> void main() { int i; for(i=1;i<=100;i++) { if((i % 2 == 0) && (i % 3 != 0) && (i % 5 != 0)) { printf("%d ",i); } } printf("\nEnd of Program"); getch(); }

/*prime numbers between 50 and 100 */ #include<stdio.h> #include<conio.h> void main() { int num,i,count,min,max; clrscr(); printf("Enter min range: "); scanf("%d",&min); printf("Enter max range: "); scanf("%d",&max); num = min; while(num<=max) { count = 0; i=2; while(i<=num/2) { if(num%i==0) { count++; break; } i++; } if(count==0 && num!= 1) printf("%d ",num); num++; } getch(); } /*count the number of times a digit is present in a number*/ #include<stdio.h> #include<conio.h> void main( )

{ int a[10],i=0,j,s,t=0,n; clrscr( ); printf("\n\nenter the four digit number:"); scanf("%d",&n); printf("enter the element to be counted"); scanf("%d",&s); do { a[i]=n%10; n=n/10; i++; }while(n>0); for(j=0;j<i;j++) { if(s==a[j]) t++; } printf("the number you entered has occured %d number of times",t); getch( ); }

/* interchange the elements of an array with the elements of another array with out using the third array */ #include<stdio.h> #include<conio.h> #define N 10 void swap (int*, int*); void main () { int a[N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int b[N] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; int i, j; clrscr(); printf("\n ARRAY BEFORE SWAP \n "); for (i = 0; i < N; i++) printf(" %d",a[i]); printf("\n"); for (i = 0; i < N; i++) printf(" %d",b[i]); for (i = 0; i < N; i++) swap (&a[i], &b[i]); printf("\n ARRAY AFTER SWAP \n"); for (i = 0; i < N; i++) printf(" %d",a[i]); printf("\n"); for (i = 0; i < N; i++) printf(" %d",b[i]); getch(); } void swap (int* a, int* b) { int temp; temp = *a; *a = *b;

*b = temp; }

/* to arrange the digits of a number in ascending order */ #include<stdio.h> #include<conio.h> void main( ) { int a[10],i=0,j,k,n; clrscr( ); printf("\n\nenter the four digit number:"); scanf("%d",&n); do { a[i]=n%10; n=n/10; i++; }while(n>0); for(j=0;j<i-1;j++) { for(k=j+1;k<i;k++) { if(a[j]>a[k]) { int temp=a[j]; a[j]=a[k]; a[k]=temp; } } } for(j=0;j<i;j++) printf(" %d ",a[j]); getch( ); }

/* to get a line of text and count the number of vowels in the text */ #include <stdio.h> #include <string.h> void main() { char str[20]; int count=0, i=0; clrscr(); printf("\nEnter any string: "); gets(str); while (str[i] != '\0') { if (str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u') count++; i++; } printf("\nNo. of vowels: %d", count);

getch(); } /* product of two matrices */ #include<stdio.h> #include<conio.h> void main() { int a[25][25],b[25][25],c[25][25],i,j,k,r,s; int m,n; clrscr(); printf("\nenter the rows and columns of A matrix...."); scanf("%d%d",&m,&n); printf("\n enter the rows and columns of B matrix...."); scanf("%d%d",&r,&s); if(m!=r) printf("\n The matrix cannot multiplied"); else { printf("\nenter the elements of A matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("\t%d",&a[i][j]); } printf("\nenter the elements of B matrix"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("\t%d",&b[i][j]); } for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<m;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("The multiplication of two matrixes"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) printf("\t%d",c[i][j]); } getch(); } /* to get a line of text and count the number of words in the text*/ #include <stdio.h> #include <string.h> void main()

{ char str[20]; int i=0, word=0; clrscr(); printf("\nEnter any string: "); gets(str); while (str[i] != '\0') { if (str[i] == ' ') { word++; } i++; } printf("\nNumber of words: %d", word+1); getch(); }

/* to find the sum and difference of two matrices */ #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],c[10][10]; int i,j,m,n,p,q; clrscr(); printf("Enter the row and column of A matrix:"); scanf("%d%d",&n,&m); printf("Enter the row and column of B matrix:"); scanf("%d%d",&p,&q); if((n==p)&&(m==q)) { printf("Matrices can be added\n"); printf("Enter the element of A matrix:\n"); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf("%d",&a[i][j]); printf("Enter the element of B matrix:\n"); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf("%d",&b[i][j]); for(i=0;i<n;++i) for(j=0;j<m;++j) c[i][j]=a[i][j]+b[i][j]; printf("sum of A and B matrix:\n"); for(i=0;i<n;++i) { for(j=0;j<m;++j) printf("%5d",c[i][j]); printf("\n"); } for(i=0;i<n;++i) for(j=0;j<m;++j) c[i][j]=a[i][j]-b[i][j]; printf("Subtraction of A and B matrix:\n"); for(i=0;i<n;++i) {

for(j=0;j<m;++j) printf("%5d",c[i][j]); printf("\n"); } getch(); } else printf("Matrices operation cannot be performed\n"); } /* to get a matrix of order 3x3 and display a matrix of order of 4x4, with the f ourth row and column as the sum of rows and columns respectively */ #include<stdio.h> #include<conio.h> void main() { int a[25][25]; int sum=0,i,j,m,n; clrscr(); printf("\nenter the row Size:"); scanf("%d",&m); printf("\nenter the column Size:"); scanf("%d",&n); printf("\nenter the elements ofa matrix"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) scanf("\t%d",&a[i][j]); } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { sum=sum+a[i][j]; } printf("sum of %d row is %d\n",i,sum); a[i][m+1]=sum; sum = 0; } for(j=1;j<=m;j++) { for(i=1;i<=n;i++) { sum=sum+a[i][j]; } printf("sum of %d column is %d\n",j,sum); a[n+1][j]=sum; sum = 0; } for(i=1;i<=m+1;i++) { for(j=1;j<=n+1;j++) { printf(" %d",a[i][j]); } printf("\n"); } getch(); }

/* to arrange the set of numbers in descending order */ #include<stdio.h> #include<conio.h> void main() { int i,j,k,n; int a[50],temp; clrscr(); printf("\n Enter the last term of the array:"); scanf("%d",&n); printf("Enter the elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("the elements in decending order\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); }

/* to duplicate element from a set of elements */ #include<stdio.h> #include<conio.h> void main() { int m,i,j,a[30]; printf("Enter the limit:"); scanf("%d",&m); printf("Enter the numbers:"); for(i=1;i<=m;i++) { scanf("%d",&a[i]); } printf("Before Duplicate:\n"); for(i=1;i<=m;i++) { printf("%d\n",a[i]); } printf("After duplicate:\n"); for(i=1,j=m+1;i<=m,j<=m*2;i++,j++) a[j]=a[i]; for(i=1;i<=m*2;i++) { printf("%d\n",a[i]); }

getch(); }

/*to find the product of two values without using * #include<stdio.h> #include<conio.h> void main() { int m1,m2; printf("enter the m1 value:"); scanf("%d",&m1); printf("Enter the m2 value:"); scanf("%d",&m2); printf("Answer:%d",mul(m1,m2)); getch(); } int mul(int m1,int m2) { int i,ans=0; for(i=1;i<=m2;i++) { ans=ans+m1; } return(ans); }

operator */

You might also like