Share this blog, if u think its helpful....
Leave your comments below, if you want any c programs
C program to find largest of three numbers :
#include<stdio.h>
int main(){
int a,b,c;
printf("\nEnter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("\nGreatest is a :%d",a);
else
if(b>c)
printf("\nGreatest is b :%d",b);
else
printf("\nGreatest is c :%d",c);
}
C program of a simple calculator :
#include<stdio.h>
int main()
{
float num1, num2, yes;
char operation;
printf("Enter the expression\n");
scanf("%f%c%f", &num1, &operation, &num2);
switch(operation)
{
case'+':
yes=num1+num2;
break;
case'-':
yes=num1-num2;
break;
case'*':
yes=num1*num2;
break;
case'/':
if (num2!=0)
yes=num1/num2;
else
printf("ERROR");
break;
default:printf("illegal operation\n");
}
printf("%f %c %f=%f\n",num1,operation,num2,yes);
}
C program to check the palindrome condition :
#include<stdio.h>
main()
{
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
C program to write the fibonocci series :
#include<stdio.h>
int main()
{
int n,i,c,a=0,b=1;
printf("Enter Fibonacci series of nth term : ");
scanf("%d",&n);
printf("%d %d ",a,b);
for(i=0;i<=(n-3);i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
}
C program to multiply two matrices :
#include<stdio.h>
int main()
{
int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
printf("Enter number of rows and columns of first matrix MAX 10\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix MAX 10\n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
printf("Enter rows and columns of First matrix \n");
printf("Row wise\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("You have entered the first matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");
}
printf("Enter rows and columns of Second matrix \n");
printf("Again row wise\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("You have entered the second matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
if(r1==r2&&c1==c2)
{
printf("Now we add both the above matrix \n");
printf("The result of the addition is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[i][j]=m1[i][j]+m2[i][j];
printf("%d\t",add[i][j]);
}
printf("\n");
}
}
else
{
printf("Addition cannot be done as rows or columns are not equal\n");
}
printf("Now we multiply both the above matrix \n");
printf("The result of the multiplication is as follows:\n");
/*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];
/*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
}
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}
else
{
printf("Matrix multiplication cannot be done");
}
}
C program to solve a quadratic equation :
#include<stdio.h>
#include<math.h>
void main()
{
float root1,root2,disc,a,b,c;
printf("enter the values of a,b andc\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if (disc>0)
{
root1=(-b+sqrt(disc)/(2*a));
root2=(-b-sqrt(disc)/(2*a));
printf("the roots are real and distinct\n");
printf("root1=%f\n root2=%f\n",root1,root2);
}
else if (disc==0)
{
root1=root2=-b/(2*a);
printf("the roots are equal\n");
printf("root1=%f\n root2=%f\n",root1,root2);
}
else
{
root1=-b/(2*a);
root2=sqrt(abs(disc)/(2*a));
printf("the roots are imaginary %f%f",root1,root2);
}
}
C program to find GCD and LCM :
#include<stdio.h>
int main()
{
int m,n,p,q,r,gcd,lcm;
printf("Enter two numbers :\n");
scanf("%d %d",&m,&n);
p=m, q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=(p*q)/gcd;
printf(" GCD(%d,%d) = %d \n",p,q,gcd);
printf(" LCM(%d,%d) = %d \n",p,q,lcm);
}
C program to check whether a number is prime or not :
#include<stdio.h>
int main()
{
int n,i,prime=0;
printf("number:\n",n);
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
prime=1;
break;
}}
if(prime==0)
printf("number is prime");
else
printf("not a prime\n");
}
C program to do a binary search :
#include<stdio.h>
int main()
{
int n,i,key,low,high,mid,a[20];
printf("Enter n:\n");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter key:\n");
scanf("%d",&key);
low=0,high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("%d is found\n",key);
}
if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
printf("not found\n");
}
C program for Bubble sort :
#include<stdio.h>
int main()
{
int n,i,j,temp,a[20];
printf("Enter the number of items:\n");
scanf("%d",&n);
printf("Enter items to sort:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;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("Items are \n");
for(j=0;j<n;j++)
printf("%d\n",a[j]);
}
Leave your comments below, if you want any c programs
C program to find largest of three numbers :
#include<stdio.h>
int main(){
int a,b,c;
printf("\nEnter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("\nGreatest is a :%d",a);
else
if(b>c)
printf("\nGreatest is b :%d",b);
else
printf("\nGreatest is c :%d",c);
}
C program of a simple calculator :
#include<stdio.h>
int main()
{
float num1, num2, yes;
char operation;
printf("Enter the expression\n");
scanf("%f%c%f", &num1, &operation, &num2);
switch(operation)
{
case'+':
yes=num1+num2;
break;
case'-':
yes=num1-num2;
break;
case'*':
yes=num1*num2;
break;
case'/':
if (num2!=0)
yes=num1/num2;
else
printf("ERROR");
break;
default:printf("illegal operation\n");
}
printf("%f %c %f=%f\n",num1,operation,num2,yes);
}
C program to check the palindrome condition :
main()
{
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
C program to write the fibonocci series :
#include<stdio.h>
int main()
{
int n,i,c,a=0,b=1;
printf("Enter Fibonacci series of nth term : ");
scanf("%d",&n);
printf("%d %d ",a,b);
for(i=0;i<=(n-3);i++)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
}
}
C program to multiply two matrices :
#include<stdio.h>
int main()
{
int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
printf("Enter number of rows and columns of first matrix MAX 10\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix MAX 10\n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
printf("Enter rows and columns of First matrix \n");
printf("Row wise\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("You have entered the first matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");
}
printf("Enter rows and columns of Second matrix \n");
printf("Again row wise\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("You have entered the second matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
if(r1==r2&&c1==c2)
{
printf("Now we add both the above matrix \n");
printf("The result of the addition is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[i][j]=m1[i][j]+m2[i][j];
printf("%d\t",add[i][j]);
}
printf("\n");
}
}
else
{
printf("Addition cannot be done as rows or columns are not equal\n");
}
printf("Now we multiply both the above matrix \n");
printf("The result of the multiplication is as follows:\n");
/*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];
/*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
}
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}
else
{
printf("Matrix multiplication cannot be done");
}
}
C program to solve a quadratic equation :
#include<stdio.h>
#include<math.h>
void main()
{
float root1,root2,disc,a,b,c;
printf("enter the values of a,b andc\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if (disc>0)
{
root1=(-b+sqrt(disc)/(2*a));
root2=(-b-sqrt(disc)/(2*a));
printf("the roots are real and distinct\n");
printf("root1=%f\n root2=%f\n",root1,root2);
}
else if (disc==0)
{
root1=root2=-b/(2*a);
printf("the roots are equal\n");
printf("root1=%f\n root2=%f\n",root1,root2);
}
else
{
root1=-b/(2*a);
root2=sqrt(abs(disc)/(2*a));
printf("the roots are imaginary %f%f",root1,root2);
}
}
C program to find GCD and LCM :
#include<stdio.h>
int main()
{
int m,n,p,q,r,gcd,lcm;
printf("Enter two numbers :\n");
scanf("%d %d",&m,&n);
p=m, q=n;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
gcd=m;
lcm=(p*q)/gcd;
printf(" GCD(%d,%d) = %d \n",p,q,gcd);
printf(" LCM(%d,%d) = %d \n",p,q,lcm);
}
C program to check whether a number is prime or not :
#include<stdio.h>
int main()
{
int n,i,prime=0;
printf("number:\n",n);
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
prime=1;
break;
}}
if(prime==0)
printf("number is prime");
else
printf("not a prime\n");
}
C program to do a binary search :
#include<stdio.h>
int main()
{
int n,i,key,low,high,mid,a[20];
printf("Enter n:\n");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter key:\n");
scanf("%d",&key);
low=0,high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("%d is found\n",key);
}
if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
printf("not found\n");
}
C program for Bubble sort :
#include<stdio.h>
int main()
{
int n,i,j,temp,a[20];
printf("Enter the number of items:\n");
scanf("%d",&n);
printf("Enter items to sort:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;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("Items are \n");
for(j=0;j<n;j++)
printf("%d\n",a[j]);
}
No comments:
Post a Comment