SOLUTIONS FOR C PROGRAM
GENERAL PROGRAMS:
Number divisible by 2 and not by 3 and 5.
#include<stdio.h> int main() { int i; printf("The numbers are"); for(i=1;i<=100;i++) if((i%3!=0)&&(i%5!=0)) { if(i%2==0) { printf("%d ",i); } } return 0; }
Number of words
#include<stdio.h> #include<string.h> int main() { char s1[50]; int word=0,i=0; printf("Enter the string\n"); gets(s1); while(s1[i]!='\0') { if(s1[i]==' ') { word++; } i++; } printf("The number of words %d",word+1);
A B, V
PK
return 0; }
String Functions (Without using Functions). LENGTH
#include<stdio.h> main() { int i,j=0; char a[100]; printf("Enter:\n"); gets(a); for(i=0;a[i]!='\0';i++) j++; printf("%d",j); return 0; }
CONCATENATION
#include<stdio.h> int main() { int i,j,k=0; printf("Enter s1: "); gets(s1); printf("Enter s2: "); gets(s2); for(i=0;s1[i]!='\0';i++)
char s1[50],s2[50],s3[50];
A B, V
PK
{ s3[i]=s1[i]; } for(j=i;s2[k]!='\0';j++) { s3[j]=s2[k]; k++; } printf("\nThe concatenation of the given string: %s",s3); return 0; }
Position search for the given number in an array.
#include<stdio.h> int main() {
int m,a[50],r,b,i;
printf("Enter the number of array elements\n"); scanf("%d",&m); for(i=0;i<m;i++) scanf("%d",&a[i]); printf("Enter the array elements\n");
printf("Enter the element to be searched in the array:\n"); scanf("%d",&b); do{ printf("position is "); for(i=0;i<m;i++) { if(b==a[i])
A B, V
PK
{printf(" %d ",i);} } printf("Enter 5 to continue"); scanf("%d",&r);} while(r==5); return 0; }
#include<stdio.h> int main() { int a=1;int m; do{ a++;
printf("%d------%c\n",a,a);
printf("press 1 to continue"); scanf("%d",&m); }while(m==1); return 0; }
Square of numbers (for number whose least significant is 5).
#include<stdio.h> int main() { int a,n; printf("Enter the number\n");
A B, V
PK
Print ASCII values one by one according to User inputs
scanf("%d",&a); n=a%10; if(n==5) {printf("The square of the given number is %d",(a*a));} else printf("This %d number doesn't have least significant digit as 5",a); return 0; }
Accept a number and print it in words.
#include<stdio.h> int main() { char s[50][50]={"one","two","three","four","five","six","seven","eight","nine"}; char t[50][50]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen"}; char u[50][50]={"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; int f,n; scanf("%d",&n); if(n<=9999) { if(n>=1000) { f=n/1000; n=n%1000; } if(n>=100) { printf("Enter the number to be printed in words\n");
printf("%s thousand and ",s[f-1]);
A B, V
PK
f=n/100; n=n%100; printf("%s hundred and",s[f-1]); } if(n>10&&n<20) { f=n%10; printf("%s ",t[f-1]); } if(n>19 && n<=99) { f=n/10; n=n%10; printf("%s ",u[f-1]); } if(n>=1&&n<=9) { printf("%s",s[n-1]); } } return 0; }
Factors of Integers.
#include<stdio.h> int main() { int n,i,rem;
printf("Enter the number\n"); scanf("%d",&n);
A B, V
PK
printf("The factors are"); for(i=1;i<=n;i++) { rem=n%i; if(rem==0) printf(" %d ",i); } return 0; }
NCR Value
#include<stdio.h> int fact(int); int main() { int n,c,r;
printf("To find ncr enter n and r"); scanf("%d%d",&n,&r);
printf("%d",(fact(n))/((fact(r)*fact(n-r)))); return 0; } int fact(int x) { if(x==0) return 1; else return(x*fact(x-1)); }
A B, V
PK
DECIMAL CONVERSION PROGRAMS:
Gray Code to Binary
#include<stdio.h> int main() { int a[10],i=0,c=0,n,b[10]; printf("\n Enter the binary code :\n"); scanf("%d",&n); { a[i]=n%10; n=n/10; i++; c++; } { b[i]=a[i]; } { if(b[i]==1) { if(a[i-1]==1) a[i-1]=0; else a[i-1]=1; } } printf("\nThe gray code is:\n"); for(i=c-1;i>=0;i--) printf("%d",a[i]); while(n!=0)
for(i=c-1;i>=0;i--)
for(i=c-1;i>=0;i--)
A B, V
PK
return 0; }
Binary to Decimal, Octal, Hexadecimal
int main() { int n,base,dec,rem; dec=0;base=1; printf("Enter the binary number:"); scanf("%d",&n); while(n>0) { rem=n%10; if(rem==1)
{dec=dec+base;} base=base*2; n=n/10; }
printf("The decimal equivalent is %d\n",dec); printf("The octal equivalent is %o\n",dec); printf("The hexadecimal equivalent is %x",dec); return 0; }
Decimal to Binary, Octal, Hexadecimal
#include<stdio.h> int main()
A B, V
PK
#include<stdio.h>
{ int n,a,base,dec,rem; dec=0;base=1; printf("Enter the decimal number:"); scanf("%d",&n); a=n; while(n>0) { rem=n%2; if(rem==1) {dec=dec+base;} base=base*10; n=n/2; } printf("The binary equivalent is %d\n",dec); printf("The octal equivalent is %o\n",a); printf("The hexadecimal equivalent is %x",a); return 0; }
Hexadecimal to Binary
#include<stdio.h> int main() { dec=0;base=1; scanf("%x",&n);
int n,a,base,dec,rem;
printf("Enter the hexadecimal number:");
printf("The decimal equivalent is %d\n",n); a=n;
A B, V
PK
while(n>0) { rem=n%2; if(rem==1) {dec=dec+base;} base=base*10; n=n/2; } printf("The binary equivalent is %d\n",dec); return 0; }
MATRIX PROGRAMS:
Matrix Multiplication.
#include<stdio.h> int main() { int r1,c1,i,j,k,r2,c2,a[50][50],b[50][50],c[50][50]; printf("Enter the size of the matrix a\n"); scanf("%d%d",&r1,&c1); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) } printf("Enter the size of b\n"); scanf("%d%d",&r2,&c2); printf("Enter the elements of a\n");
scanf("%d",&a[i][j]);
A B, V
PK
if(c1!=r2) printf("matrix multiplication is not possible"); else { printf("Enter the elements of b\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&b[i][j]); } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0;
for(k=0;k<c1;k++) } }
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf("after multiplication the matrix is\n"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { printf("%d\t",c[i][j]);} printf("\n"); } } return 0;}
A B, V
PK
Matrix Addition.
#include<stdio.h> int main() { int r,c1,i,j,k,a[50][50],b[50][50],c[50][50]; printf("Enter the size of the matrix a\n"); scanf("%d%d",&r,&c1); printf("Enter the elements of a\n"); for(i=0;i<r;i++) { for(j=0;j<c1;j++) scanf("%d",&a[i][j]); } printf("Enter the elements of b\n"); for(i=0;i<r;i++) { for(j=0;j<c1;j++) scanf("%d",&b[i][j]); } c[i][j]=0; for(i=0;i<r;i++) {
for(j=0;j<c1;j++) } for(i=0;i<r;i++) { for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
printf("The add is\n");
{printf("%d ",c[i][j]);}
A B, V
PK
printf("\n"); } return 0; } Matrix subraction is similar to this
Matrix Transpose of a square matrix
#include<stdio.h> int main() { int i,j,r,k,c,a[50][50],b[50][50]; printf("Enter the size of matrix a"); scanf("%d%d",&r,&c); printf("Enter the elements of a"); { for(j=0;j<c;j++) scanf("%d",&a[i][j]);} b[i][j]=0; for(i=0;i<r;i++) { for(j=0;j<c;j++) b[i][j]=a[j][i]; }
printf("The transposed matrix is\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",b[i][j]);
A B, V
PK
for(i=0;i<r;i++)
printf("\n");} return 0; }
To check whether the given matrix is a Triangular Matrix or not
#include<stdio.h> int main() { int r1,x=0,z=0,c1,i,j,k,a[50][50]; printf("Enter the size of the matrix a\n"); scanf("%d%d",&r1,&c1); printf("Enter the elements of a\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&a[i][j]); }
for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { x++; } } } if(x==3)
{if(j>i && a[i][j]==0)
printf("lower triangular matrix");
A B, V
PK
else printf("not a lower triangular matrix"); return 0;} Upper triangular matrix is similar to this
Largest number in Row and in Column in a matrix
#include<stdio.h> int main() { int i,big,j,r,c,a[50][50],sum; printf("Enter the size of matrix a"); scanf("%d%d",&r,&c); printf("Enter the elements of a"); for(i=0;i<r;i++) { for(j=0;j<c;j++) } for(i=0;i<r;i++) {big=a[0][0]; for(j=0;j<c;j++) {if(a[i][j]>big) {big=a[i][j];}} printf("\n\n"); for(j=0;j<c;j++) {big=a[0][0]; for(i=0;i<r;i++) {if(a[i][j]>big) scanf("%d",&a[i][j]);
printf("\nThe biggest in %d row number is %d",i+1,big);}
A B, V
PK
{big=a[i][j];}} printf("\nThe biggest in %d column number is %d",j+1,big);} return 0; }
Interchange 2 matrices without another matrix.
#include<stdio.h> int main() { int r1,c1,i,j,k,a[50][50],b[50][50]; printf("Enter the size of the matrix a\n"); scanf("%d%d",&r1,&c1); printf("Enter the elements of a\n"); for(i=0;i<r1;i++) { scanf("%d",&a[i][j]); } printf("Enter the elements of b\n"); for(i=0;i<r1;i++) {
for(j=0;j<c1;j++) } { {
scanf("%d",&b[i][j]); for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
a[i][j]=a[i][j]-b[i][j]; b[i][j]=a[i][j]+b[i][j];
A B, V
PK
for(j=0;j<c1;j++)
a[i][j]=b[i][j]-a[i][j]; } } printf("the elements of a\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%d\t",a[i][j]); } printf("\n"); } for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { } } return 0; } printf("%d\t",b[i][j]); printf("\n");
Row sum and Column sum Matrix.
#include<stdio.h> int main() { int i,j,r,c,a[50][50],sum; printf("Enter the size of matrix a\n");
A B, V
PK
printf("the elements of b\n");
scanf("%d%d",&r,&c); printf("Enter the elements of a\n"); for(i=1;i<=r;i++) { for(j=1;j<=c;j++) scanf("%d",&a[i][j]); } for(i=1;i<=r;i++) { for(j=1;j<=c;j++) {sum=sum+a[i][j];} sum=0; } { printf("\nThe sum of %d row is %d",i,sum);
for(j=1;j<=c;j++) for(i=1;i<=r;i++)
{sum=sum+a[i][j];} sum=0;} return 0;}
printf("\nThe sum of %d column is %d",j,sum);
EASY PROGRAMS:
Celsius -> Farenheit, Farenheit-> Celsius.
#include<stdio.h> int main(void) { float c,f; printf("celsius");
A B, V
PK
sum=0;
scanf("%f",&c); f=(1.8*c)+32; printf("farenheit %f",f); return 0;}
Leap year or not
#include<stdio.h> int main() int year; printf("Enter the year\n"); scanf("%d",&year); if(year%4==0) else return 0; } printf("The %d is leap",year); {
printf("The %d is not a leap",year);
Multiplication without using * operator
#include<stdio.h> int main() { int a,b,i,sum=0; printf("Enter values for a and b in a*b "); scanf("%d%d",&a,&b); for(i=1;i<=b;i++) {
A B, V
PK
sum=sum+a; } printf("%d*%d=%d",a,b,sum); return 0; }
Ascending order of an array
#include<stdio.h> int main() { int a[100],n,temp,i,j; scanf("%d",&n); for(i=0;i<n;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("\nThe elements in ascending order is\n"); for(i=0;i<n;i++)
printf("Enter the size of array\n");
printf("Enter the elements of the array\n"); scanf("%d",&a[i]);
A B, V
PK
printf("%d ",a[i]); return 0;}
Descending order of an array
#include<stdio.h> int main() { int a[50],n,i,t,j; printf("Enter the size of array\n"); scanf("%d",&n); printf("Enter the elements of the array\n"); scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=n-1;j>0;j--) { { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("sorted array elements are\n"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; } if(a[i]<a[j])
A B, V
PK
for(i=0;i<n;i++)
Armstrong number
#include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num!=0) { r=num%10; sum=sum+(r*r*r); } if(sum==temp) else return 0; } num=num/10;
printf("%d is not an Armstrong number",temp);
Factorial (Recursive function )
#include<stdio.h> int fact(int n); main() { int n; printf("Enter the number\n"); scanf("%d",&n);
A B, V
printf("%d is an Armstrong number",temp);
PK
printf("factorial of %d is %d",n,fact(n)); return 0; } int fact(int n) { if(n==0) return 1; else return(n*fact(n-1)); }
Sum of Odd and Even Position of a number
#include<stdio.h> int main(void) { int A[10],n,sum=0,z=0,N,dig,i,j,temp; printf("Enter the number: "); scanf("%d",&N); n=0; while(N>0) { dig=N%10; A[n]=dig; N=N/10; n++; } for(i=0;i<n;i++) {
A B, V
PK
if(i%2==0) { sum=sum+A[i]; } else { z=z+A[i]; } } printf("\nThe sum of elements in the even position is %d",sum); printf("\nThe sum of elements in the odd position is %d",z); return 0; }
Print Prime numbers between 1 to 100
#include<stdio.h> int main(void) { int n,c=0,a,z;
printf("enter number up to which prime number is to be found: "); scanf("%d",&a); for(c=0;a>0;a--) {n=a; for(c=0;n>0;n--) { z=(a%n); if (z==0) c++; printf("prime numbers are: ");
A B, V
PK
} if (c==2) printf("%d,",a); } return 0; }
Reverse of number (checking whether it is a palindrome or not)
#include<stdio.h> int main() { int a,reverse=0,n; printf("Enter the number:"); scanf("%d",&a); n=a; { reverse=reverse*10; reverse=reverse+(a%10); a=a/10;} if(reverse==n) while(a!=0)
{printf("%d is a palindrome",n);} return 0;}
printf("\nThe reverse is %d",reverse);
Palindrome ( Without using String )
#include<stdio.h> main()
A B, V
PK
{ int i,j=0; char a[100]; printf("Enter:\n"); gets(a); for(i=0;a[i]!='\0';i++) j++; for(i=0;i<j;i++) { if(a[i]==a[j-(i+1)]) { if(i==j/2) printf("yes"); continue; } else {printf("no"); break;} } }
Fibonacci
#include<stdio.h> void fib(int n); int main(void) { int n;
printf("Enter the number of terms"); scanf("%d",&n); fib(n);
A B, V
PK
return 0;} void fib(int n) { int f,s,z,i; f=0;s=1; printf("The fibonacci series is %d %d ",f,s); for(i=0;i<n-2;i++) { z=f+s; printf("%d ",z); f=s; s=z;} }
Pascals Triangle.
#include<stdio.h> int fact(int); int main() { int i,k,n;
printf("Enter the number of rows to print: "); scanf("%d",&n); { for(i=0; i<n; i++)
for(k=0; k<=(n-i-2); k++) printf(" "); for(k=0; k<=i; k++) printf("\n");
printf("%d ",(fact(i))/(fact(k)*fact(i-k)));
A B, V
PK
} return 0;} int fact(int x) { if(x==0) else return(x*fact(x-1)); } return (1);
Floyds Triangle.
#include<stdio.h> int main() { int a=1,i,j,n;
printf("Enter the number of rows to print: "); scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<=i;j++) { printf("%d ",a); a++; } printf("\n"); } return 0; }
A B, V
PK
Sorting of names in alphabetical order using arrays.
#include<stdio.h> #include<string.h> int main() { char s[50][50],t[50]; int i,n,j; printf("Enter the total number of words: "); scanf("%d",&n); printf("Enter the words\n"); {gets(s[i]);} for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { { for(i=0;i<=n;i++)
if((strcmp(s[j-1],s[j]))>0) strcpy(t,s[j-1]); strcpy(s[j],t); } } }
strcpy(s[j-1],s[j]);
printf("The arranged manner is \n"); for(i=0;i<=n;i++) printf("\n%s",s[i]); return 0;}
A B, V
PK
Convert Lower case to Upper case
#include<stdio.h> void upper(char str[50]); int main(void) { char str[50]; printf("Enter string:"); gets(str); upper(str); return 0; } void upper(char str[50]) { int i; for(i=0;str[i];i++) str[i]-=32; } if(str[i]>96&&str[i]<123)
printf("The uppercase is %s,",str);
Swapping (without using variable)
#include<stdio.h> main() { int a,b,i,n; printf("Enter a,b:"); scanf("%d%d",&a,&b); a=a-b; b=a+b;
A B, V
PK
a=b-a; printf("\na=%d\tb=%d",a,b); return 0; }
PROGRAMS:
( for which output is given) 1 1 2
1 #include<stdio.h> int main() { int i,k,n;
printf("Enter the number of rows to print: "); for(i=0;i<n;i++) { for(k=0;k<=(n-i-2);k++) printf(" "); for(k=0;k<=i;k++) printf("%d ",k+1); printf("\n"); }return 0; }
A B, V
5 5 5
PK
scanf("%d",&n);
#include<stdio.h> int main() { int i,k,n,m; printf("Enter the number of rows to print:"); scanf("%d",&n); printf("Enter the element to be printed on that triangle:"); scanf("%d",&m); for(i=0;i<n;i++) { for(k=0;k<=(n-i-2);k++) printf(" "); for(k=0;k<=i;k++) printf("%d ",m); printf("\n"); }
Some codes are not listed here. Sorry for that. VPK-Deepak VPK AB-Ajay Balaji
A B, V
PK
}return 0;