Tuesday 14 February 2012

how to multipli two matrix


#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k;
printf("enter the order of first matrix(m,n):");
scanf("%d %d",&m,&n);
printf("enter the order of second matrix(p,q):");
scanf("%d %d",&p,&q);
if(n!=p)
printf("multiplication  is not possible");
else
{
printf("enter the elements of first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter elelmets(%d,%d)\n",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nenter the elemets of second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("enter elelmets of (%d,%d)",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);

}
printf("\n");
}
printf("second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("\t%d",b[i][j]);

}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("multiplication of matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("\t%d",c[i][j]);
printf("\n");
}
}
}

how to add two matrix


#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
printf("enter the order of first matrix(m,n):");
scanf("%d %d",&m,&n);
printf("enter the order of second matrix(p,q):");
scanf("%d %d",&p,&q);
if(p!=m||q!=n)
printf("addetion is not possible");
else
{
printf("enter the elements of first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter elelmets(%d,%d)\n",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nenter the elemets of second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("enter elelmets of (%d,%d)",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("\t%d",b[i][j]);
c[i][j]=a[i][j]+b[i][j];
}
printf("\n");
}
printf("sum of matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("\t%d",c[i][j]);
printf("\n");
}
}
}

Friday 10 February 2012

how to check the given number is amstrong or not


#include<stdio.h>
#include<math.h>
main()
{
int a,i,n,sum=0,am;
printf("enter your number:");
scanf("%d",&n);
a=n;
while(n>0)
{
i=n%10;
sum=sum+pow(i,3);
n=n/10;
}
if(sum==a)
printf("%d is amstrong",a);
else
printf("%d is not amstrong",a);
}

how to find largest and smallest from a set of numbers


#include<stdio.h>
main()
{
int a[30],i,large,small,n;
printf("enter your limit\t");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
small=large=a[0];
for(i=0;i<n;i++)
{
if(a[i]<small)
small=a[i];
if(a[i]>large)
large=a[i];
}
printf("large=%d",large);
printf("small=%d",small);
}

how to print * patern


#include<stdio.h>
void main()
{
int a,b,c,i,j,k;
printf("no of lins:");
scanf("%d",&a);
for(i=0;i<a;i++)
{
for(j=32;j>i;j--)
{
printf(" ");
}
for(b=0;b<=i;b++)
{
printf(" *");
}
printf("\n");

}
}

to find the riverse of a number


#include<stdio.h>
main()
{
int a,b,rev=0,temp;
printf("enter your number: ");
scanf("%d",&a);
temp=a;
while(a>0)
{
b=a%10;
rev=rev*10+b;
a=a/10;
}
printf("\nreverse of %d is %d",temp,rev);
}

how to solve quadratic equation


#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float r1,r2,re,im,p,q,x,y;
printf("enter the coeficiants of ur quadratic equation\n  ");
scanf("%d %d %d",&a,&b,&c);
if(a==0)
printf("this is not a quadratic equation");
else
{
p=b*b-4*a*c;
if(p==0)
{
printf("there is only one root\n");
r1=-b/(2.0*a);
printf("\tthe root is =%f",r1);
}
if(p<0)
{
q=-p;
re=sqrt(q)/(2*a);
im=-b/(2.0*a);
printf("\tthe roots are imaginary\n");
printf("\troot 1= %f +i %f\n",re,im);
printf("\troot 2=%f -i %f",re,im);
}
if(p>0)
{
x=(-b+sqrt(p))/(2*a);
y=(-b-sqrt(p))/(2*a);
printf("\nroot 1=%f",x);
printf("\nroot 2=%f",y);
}
}
}

how to sort array with fractional numbers


#include<stdio.h>
main()
{
int i,n,j;
float temp,a[10];
printf("enter the limit:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter %d of %d number\n",i+1,n);
scanf("%f",&a[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("sorted numbers are\n");
for(i=0;i<n;i++)

printf("%f\t",a[i]);
}

how to check wether the given number is prime or not


#include<stdio.h>
#include<math.h>
main()
{
int i,flag,n;
printf("enter your number:");
scanf("%d",&n);
flag=1;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==0)
printf("\n %d is not a prime number",n);


else
printf("\n %d is a prime number",n);
}

how to find fibonacci numbers upto a limit


#include<stdio.h>
main()
{
int a=1,b=0,c=0,n;
printf("enter the limit:");
scanf("%d",&n);
while(c<n)
{
printf("\t%d",c);
c=a+b;
a=b;
b=c;
}
}