19/5/11 - Electronics II
21/5/11 - Electromagnetics
24/5/11 - Maths
26/5/11 - Fluid Mechanics
28/5/11 - Applied thermodynamics
Search This Blog
Friday, April 29, 2011
Friday, April 22, 2011
CODE 01
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 1. Addition of two numbers
#include
#include
int main ()
{
float a,b,c;
printf("ADDITION OF TWO NUMBERS");
printf("\nEnter first number : ");
scanf("%f",&a);
printf("\nEnter second number : ");
scanf("%f",&b);
c=a+b;
printf("\nThe sum of two numbers is : %f",c);
getch();
return 0;
}
//QUESTION 1. Addition of two numbers
#include
#include
int main ()
{
float a,b,c;
printf("ADDITION OF TWO NUMBERS");
printf("\nEnter first number : ");
scanf("%f",&a);
printf("\nEnter second number : ");
scanf("%f",&b);
c=a+b;
printf("\nThe sum of two numbers is : %f",c);
getch();
return 0;
}
CODE 02
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 2. Find greatest of three numbers
#include
#include
int main ()
{
float a,b,c;
printf("GREATEST OF THREE NUMBERS");
printf("Enter first number : ");
scanf("%f",&a);
printf("\nEnter second number : ");
scanf("%f",&b);
printf("\nEnter third number : ");
scanf("%f",&c);
if((a>b)&&(a>c))
printf("\nThe greatest of three numbers is : %f",a);
else if((b>a)&&(b>c))
printf("\nThe greatest of three numbers is : %f",b);
else
printf("\nThe greatest of three numbers is : %f",c);
getch();
return 0;
}
//QUESTION 2. Find greatest of three numbers
#include
#include
int main ()
{
float a,b,c;
printf("GREATEST OF THREE NUMBERS");
printf("Enter first number : ");
scanf("%f",&a);
printf("\nEnter second number : ");
scanf("%f",&b);
printf("\nEnter third number : ");
scanf("%f",&c);
if((a>b)&&(a>c))
printf("\nThe greatest of three numbers is : %f",a);
else if((b>a)&&(b>c))
printf("\nThe greatest of three numbers is : %f",b);
else
printf("\nThe greatest of three numbers is : %f",c);
getch();
return 0;
}
CODE 03
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 3. Find the solution of a given Quadratic Equation.
#include
#include
#include
int main ()
{
float a,b,c;
printf("Let the eqation be ax^2 + bx + c");
printf("\nEnter a : ");
scanf("%f",&a);
printf("\nEnter b : ");
scanf("%f",&b);
printf("\nEnter c : ");
scanf("%f",&c);
float d;
d=b*b-4*a*c;
float x,y;
if((d>0)||(d==0))
{
printf("\nThe roots are real : ");
x=(((-1)*b+sqrt(d))/(2*a));
printf("\n");
printf("%f",x);
y=(((-1)*b-sqrt(d))/(2*a));
printf("\n");
printf("%f",y);
}
else
{
printf("\nThe roots are not real : ");
d=sqrt((-1)*d);
x=(((-1)*b)/(2*a));
printf("\n");
printf("%f",x);
printf(" + ");
printf("i");
printf("%f",d);
y=(((-1)*b)/(2*a));
printf("\n");
printf("%f",x);
printf(" - ");
printf("i");
printf("%f",d);
}
getch();
return 0;
}
//QUESTION 3. Find the solution of a given Quadratic Equation.
#include
#include
#include
int main ()
{
float a,b,c;
printf("Let the eqation be ax^2 + bx + c");
printf("\nEnter a : ");
scanf("%f",&a);
printf("\nEnter b : ");
scanf("%f",&b);
printf("\nEnter c : ");
scanf("%f",&c);
float d;
d=b*b-4*a*c;
float x,y;
if((d>0)||(d==0))
{
printf("\nThe roots are real : ");
x=(((-1)*b+sqrt(d))/(2*a));
printf("\n");
printf("%f",x);
y=(((-1)*b-sqrt(d))/(2*a));
printf("\n");
printf("%f",y);
}
else
{
printf("\nThe roots are not real : ");
d=sqrt((-1)*d);
x=(((-1)*b)/(2*a));
printf("\n");
printf("%f",x);
printf(" + ");
printf("i");
printf("%f",d);
y=(((-1)*b)/(2*a));
printf("\n");
printf("%f",x);
printf(" - ");
printf("i");
printf("%f",d);
}
getch();
return 0;
}
CODE 04
//GAURAV MITTAL(2K9/EE/830)
/*QUESTION 4. FIND THE SUM OF SERIES : i)e^x
ii)sin(x)
iii)cos(x)
*/
#include
#include
int main()
{
float x,xx,n,nn;
float a=0,sum=1;
int sign,i,j;
printf("\n\nEnter the value of 'x' : ");
scanf("%f",&x);
//EXPONENTIAL SERIES
printf("\n\n\n\nEnter the order(maximum power of'x') 'n' of exponential series : ");
scanf("%f",&n);
for(i=0;i {
xx=1;
nn=1;
for(j=0;j {
xx=xx*x;
nn=nn*(j+1);
a=xx/nn;
}
sum=sum+a;
}
printf("\ne^x till order 'n' is : ");
printf("%f",sum);
//SINE SERIES
sign=1;
sum=0;
printf("\n\n\n\nEnter the order(maximum power of'x') 'n'(odd value) of sine series : ");
scanf("%f",&n);
for(i=0;i {
xx=1;
nn=1;
if((i+1)%2==0)
{
for(j=0;j {
xx=xx*x;
nn=nn*(j+1);
a=xx/nn;
}
if(sign==1)
{
sum=sum+a;
sign=0;
}
else
{
sum=sum-a;
sign=1;
}
}
}
printf("\nsin(x) till order 'n' is : ");
printf("%f",sum);
//COS SERIES
sign=1;
sum=1;
printf("\n\n\n\nEnter the order(maximum power of'x') 'n'(even value) of cos series : ");
scanf("%f",&n);
for(i=0;i {
xx=1;
nn=1;
if((i+1)%2==1)
{
for(j=0;j {
xx=xx*x;
nn=nn*(j+1);
a=xx/nn;
}
if(sign==1)
{
sum=sum+a;
sign=0;
}
else
{
sum=sum-a;
sign=1;
}
}
}
printf("\nCos(x) till order 'n' is : ");
printf("%f",sum);
getch();
return 0;
}
/*QUESTION 4. FIND THE SUM OF SERIES : i)e^x
ii)sin(x)
iii)cos(x)
*/
#include
#include
int main()
{
float x,xx,n,nn;
float a=0,sum=1;
int sign,i,j;
printf("\n\nEnter the value of 'x' : ");
scanf("%f",&x);
//EXPONENTIAL SERIES
printf("\n\n\n\nEnter the order(maximum power of'x') 'n' of exponential series : ");
scanf("%f",&n);
for(i=0;i
xx=1;
nn=1;
for(j=0;j {
xx=xx*x;
nn=nn*(j+1);
a=xx/nn;
}
sum=sum+a;
}
printf("\ne^x till order 'n' is : ");
printf("%f",sum);
//SINE SERIES
sign=1;
sum=0;
printf("\n\n\n\nEnter the order(maximum power of'x') 'n'(odd value) of sine series : ");
scanf("%f",&n);
for(i=0;i
xx=1;
nn=1;
if((i+1)%2==0)
{
for(j=0;j {
xx=xx*x;
nn=nn*(j+1);
a=xx/nn;
}
if(sign==1)
{
sum=sum+a;
sign=0;
}
else
{
sum=sum-a;
sign=1;
}
}
}
printf("\nsin(x) till order 'n' is : ");
printf("%f",sum);
//COS SERIES
sign=1;
sum=1;
printf("\n\n\n\nEnter the order(maximum power of'x') 'n'(even value) of cos series : ");
scanf("%f",&n);
for(i=0;i
xx=1;
nn=1;
if((i+1)%2==1)
{
for(j=0;j {
xx=xx*x;
nn=nn*(j+1);
a=xx/nn;
}
if(sign==1)
{
sum=sum+a;
sign=0;
}
else
{
sum=sum-a;
sign=1;
}
}
}
printf("\nCos(x) till order 'n' is : ");
printf("%f",sum);
getch();
return 0;
}
CODE 05
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 5. Arrange a given set of numbers in array in ascending order.
#include
#include
int main()
{
int x[20],y[20];
int n,i,j;
int smaller,prev=0;
printf("\n\nEnter the number of terms : ");
scanf("%d",&n);
printf("\n\n\n");
for(i=0;i {
printf("Term no. %d",i+1," : ");
printf(" : ");
scanf("%d",&x[i]);
}
for(i=0;i {
smaller=32767;
for(j=0;j {
if((x[j]prev))
{
smaller=x[j];
}
}
y[i]=smaller;
prev=smaller;
}
printf("\n\n\nTerms in ascending order :");
for(i=0;i {
printf("\nTerm no. %d",i+1);
printf(" : ");
printf("%d",y[i]);
}
getch();
return 0;
}
//QUESTION 5. Arrange a given set of numbers in array in ascending order.
#include
#include
int main()
{
int x[20],y[20];
int n,i,j;
int smaller,prev=0;
printf("\n\nEnter the number of terms : ");
scanf("%d",&n);
printf("\n\n\n");
for(i=0;i
printf("Term no. %d",i+1," : ");
printf(" : ");
scanf("%d",&x[i]);
}
for(i=0;i
smaller=32767;
for(j=0;j
if((x[j]
{
smaller=x[j];
}
}
y[i]=smaller;
prev=smaller;
}
printf("\n\n\nTerms in ascending order :");
for(i=0;i
printf("\nTerm no. %d",i+1);
printf(" : ");
printf("%d",y[i]);
}
getch();
return 0;
}
CODE 06
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 6. Find the minimum and maxuimum out of given set of numbers.
#include
#include
int main()
{
int x[20];
int n,i;
int smallest=32767,largest=0;
printf("\n\nEnter the number of terms : ");
scanf("%d",&n);
printf("\n\n\n");
for(i=0;i {
printf("Term no. %d",i+1," : ");
printf(" : ");
scanf("%d",&x[i]);
}
for(i=0;i {
if(x[i] smallest=x[i];
if(x[i]>largest)
largest=x[i];
}
printf("\nLargest Term %d",largest);
printf("\nSmallest Term %d",smallest);
getch();
return 0;
}
//QUESTION 6. Find the minimum and maxuimum out of given set of numbers.
#include
#include
int main()
{
int x[20];
int n,i;
int smallest=32767,largest=0;
printf("\n\nEnter the number of terms : ");
scanf("%d",&n);
printf("\n\n\n");
for(i=0;i
printf("Term no. %d",i+1," : ");
printf(" : ");
scanf("%d",&x[i]);
}
for(i=0;i
if(x[i]
if(x[i]>largest)
largest=x[i];
}
printf("\nLargest Term %d",largest);
printf("\nSmallest Term %d",smallest);
getch();
return 0;
}
CODE 07
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 7. Find the transpose and multiply two matrices.
#include
#include
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,l,m,n;
printf("\n\n\n\n\n\n\nEnter the no.of rows for MATRIX A :");
scanf("%d",&l);
printf("\nEnter the no.of columns for MATRIX A :");
scanf("%d",&m);
for(i=0;i {
for(j=0;j {
printf("\nEnter element A[%d",i+1);
printf("][%d",j+1);
printf("] : ");
scanf("%d",&a[i][j]);
}
}
printf("\n\n\nEnter the no.of columns for MATRIX B :");
scanf("%d",&n);
for(i=0;i {
for(j=0;j {
printf("\nEnter element B[%d",i+1);
printf("][%d",j+1);
printf("] : ");
scanf("%d",&b[i][j]);
}
}
printf("\n\n\n\t\tMATRIX A :\n");
for(i=0;i {
printf("\n");
for(j=0;j {
printf("\t");
printf("%d",a[i][j]);
}
}
printf("\n\n\n\t\tMATRIX B :\n");
for(i=0;i {
printf("\n");
for(j=0;j {
printf("\t");
printf("%d",b[i][j]);
}
}
printf("\n\n\n\n\nThe transpose of MATRIX A is :\n");
for(j=0;j {
printf("\n");
for(i=0;i {
printf("\t");
printf("%d",a[i][j]);
}
}
printf("\n\n\nThe transpose of MATRIX B is :\n");
for(j=0;j {
printf("\n");
for(i=0;i {
printf("\t");
printf("%d",b[i][j]);
}
}
for(i=0;i {
for(j=0;j {
c[i][j]=0;
for(k=0;k {
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("\n\n\n\n\nThe product of the two matrices is :\n");
for(i=0;i {
printf("\n");
for(j=0;j {
printf("\t");
printf("%d",c[i][j]);
}
}
getch();
return 0;
}
//QUESTION 7. Find the transpose and multiply two matrices.
#include
#include
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,l,m,n;
printf("\n\n\n\n\n\n\nEnter the no.of rows for MATRIX A :");
scanf("%d",&l);
printf("\nEnter the no.of columns for MATRIX A :");
scanf("%d",&m);
for(i=0;i
for(j=0;j
printf("\nEnter element A[%d",i+1);
printf("][%d",j+1);
printf("] : ");
scanf("%d",&a[i][j]);
}
}
printf("\n\n\nEnter the no.of columns for MATRIX B :");
scanf("%d",&n);
for(i=0;i
for(j=0;j
printf("\nEnter element B[%d",i+1);
printf("][%d",j+1);
printf("] : ");
scanf("%d",&b[i][j]);
}
}
printf("\n\n\n\t\tMATRIX A :\n");
for(i=0;i
printf("\n");
for(j=0;j
printf("\t");
printf("%d",a[i][j]);
}
}
printf("\n\n\n\t\tMATRIX B :\n");
for(i=0;i
printf("\n");
for(j=0;j
printf("\t");
printf("%d",b[i][j]);
}
}
printf("\n\n\n\n\nThe transpose of MATRIX A is :\n");
for(j=0;j
printf("\n");
for(i=0;i
printf("\t");
printf("%d",a[i][j]);
}
}
printf("\n\n\nThe transpose of MATRIX B is :\n");
for(j=0;j
printf("\n");
for(i=0;i
printf("\t");
printf("%d",b[i][j]);
}
}
for(i=0;i
for(j=0;j
c[i][j]=0;
for(k=0;k
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("\n\n\n\n\nThe product of the two matrices is :\n");
for(i=0;i
printf("\n");
for(j=0;j
printf("\t");
printf("%d",c[i][j]);
}
}
getch();
return 0;
}
CODE 08
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 8. Newton Raphson Method
// f(x)= 3x-cos(x)-1
// solution to f(x) is x=0.607101648
#include
#include
#include
int main()
{
int i,itr;
float x,y,dy,x1;
printf("\nNewton Raphson Method");
printf("\nf(x)= 3x-cos(x)-1");
printf("\n\n\nEnter approximate value of 'x' : ");
scanf("%f",&x);
printf("\n\nEnter number of iterations : ");
scanf("%d",&itr);
printf("\n\n\n\n\n");
for(i=1;i<=itr;i++)
{
y=(3*(x)-cos(x)-1);
dy=(3+sin(x));
x1=(x-(y/dy));
printf("\n\tx%d = %f",i,x1);
x=x1;
}
printf("\n\n\n\nAfter %d iterations, the value of 'x' is %f",itr,x);
printf("\n\nFYI, the solution is x = 0.607101648");
getch();
return 0;
}
//QUESTION 8. Newton Raphson Method
// f(x)= 3x-cos(x)-1
// solution to f(x) is x=0.607101648
#include
#include
#include
int main()
{
int i,itr;
float x,y,dy,x1;
printf("\nNewton Raphson Method");
printf("\nf(x)= 3x-cos(x)-1");
printf("\n\n\nEnter approximate value of 'x' : ");
scanf("%f",&x);
printf("\n\nEnter number of iterations : ");
scanf("%d",&itr);
printf("\n\n\n\n\n");
for(i=1;i<=itr;i++)
{
y=(3*(x)-cos(x)-1);
dy=(3+sin(x));
x1=(x-(y/dy));
printf("\n\tx%d = %f",i,x1);
x=x1;
}
printf("\n\n\n\nAfter %d iterations, the value of 'x' is %f",itr,x);
printf("\n\nFYI, the solution is x = 0.607101648");
getch();
return 0;
}
CODE 09(a)
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 8. TRAPEZOIDAL RULE
// f(x)= 3x^2 + 4
#include
#include
float z;
float f(float x)
{
z=(3*x*x)+4;
return z;
}
int main()
{
float h,value,m,a,b,n,i;
printf("\n\nTRAPEZOIDAL RULE");
printf("\n\nf(x)= 3x^2 + 4");
printf("\n\n\n\nEnter value of a (lower limit) : ");
scanf("%f",&a);
printf("\n\nEnter value of b (upper limit) : ");
scanf("%f",&b);
printf("\n\nEnter number of divisions 'n' : ");
scanf("%f",&n);
h=((b-a)/n);
m=a;
value=f(m);
for(i=1;i
{
m=m+h;
value=(value+(2*f(m)));
}
m=b;
value=value+f(m);
value=(value*h/2);
printf("\n\n\nThe area of the given equation within limits %f to %f is : %f",a,b,value);
getch();
return 0;
}
CODE 09(b)
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 11. SIMPSONS RULE
// f(x)= 3x^2 + 4
#include
#include
#include
float z;
float f(float x)
{
z=(3*x*x)+4;
return z;
}
int main()
{
int i;
float h,value,m,a,b,n;
printf("\n\nSIMPSONS RULE");
printf("\n\nf(x)= 3x^2 + 4");
printf("\n\n\n\nEnter value of a (lower limit) : ");
scanf("%f",&a);
printf("\n\nEnter value of b (upper limit) : ");
scanf("%f",&b);
printf("\n\nEnter number of divisions 'n' : ");
scanf("%f",&n);
h=((b-a)/n);
m=a;
value=f(m);
for(i=1;i
{
m=m+h;
if((i+2)%2==1)
value=(value+(4*f(m)));
else
value=(value+(2*f(m)));
}
m=b;
value=value+f(m);
value=(value*h/3);
printf("\n\n\nThe area of the given equation within limits %f to %f is : %f",a,b,value);
getch();
return 0;
}
CODE 10
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 10. To find out the unknowns (x,y,z) using Gauss elimination method
#include"stdio.h"
#include"conio.h"
int main()
{
int i,j;
float a[3][4],b[3][4],c[4][4];
float x,y,z,p,q,r;
m:
printf("\nEnter the coefficients: ");
printf("\n\n");
for(i=0;i<3;i++)
{
printf("\n");
printf("Enter a1 : ");
scanf("%f",&a[i][0]);
printf("Enter b1 : ");
scanf("%f",&a[i][1]);
printf("Enter c1 : ");
scanf("%f",&a[i][2]);
}
printf("\n\nEnter the constants: ");
printf("\n\n");
printf("Enter d1 : ");
scanf("%f",&a[0][3]);
printf("Enter d2 : ");
scanf("%f",&a[1][3]);
printf("Enter d3 : ");
scanf("%f",&a[0][3]);
if(a[0][0]!=0.0)
{
p=a[1][0];
q=a[0][0];
r=a[2][0];
for(j=0;j<=3;j++)
{
b[0][j]=-(p/q)*a[0][j];
a[1][j]+=b[0][j];
c[0][j]=-(r/q)*a[0][j];
a[2][j]+=c[0][j];
}
p=a[2][1];
q=a[1][1];
for(j=0;j<=3;j++)
{
b[1][j]=-(p/q)*a[1][j];
a[2][j]+=b[1][j];
}
printf("\n\nThe matrix becomes\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%.4f\t",a[i][j]);
}
printf("\n");
}
z=a[2][3]/a[2][2];
y=(a[1][3]-a[1][2]*z)/a[1][1];
x=(a[0][3]-a[0][2]*z-a[0][1]*y)/a[0][0];
printf("\n\n\nThe solution is\n");
printf("\nX=%f,\tY=%f,\tZ=%f",x,y,z);
}
else
{
printf("\nThe first cofficient must not be zero,Enter again");
goto m;
}
getch();
return 0;
}
CODE 11
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 11. Runge-Kutta Method.
// dy/dx = (y^2-2x)/(y^2+x).
#include
#include
float z;
float f(float x,float y)
{
z=(y*y-2*x)/(y*y+x);
return z;
}
int main()
{
float x0, y0, h, xn, k[5];
printf("\n\nRunge-Kutta Method");
printf("\n\ndy/dx = (y^2-2x)/(y^2+x)");
printf ("\n\n\n\n\n\nEnter the values of 'x0' : ");
scanf ("%f",&x0);
printf ("\nEnter the values of 'y0' : ");
scanf ("%f",&y0);
printf ("\nEnter the increment in x i.e. 'h' : ");
scanf ("%f",&h);
printf ("\nEnter the nth value of x i.e. 'xn' : ");
scanf ("%f",&xn);
printf("\n\n\n\n");
do
{
k[0] = h*f(x0, y0);
k[1] = h*f((x0+(h/2)),(y0+(k[0]/2)));
k[2] = h*f((x0+(h/2)),(y0+(k[1]/2)));
k[3] = h*f((x0+h),(y0+k[2]));
k[4] = ((k[0]+(2*(k[1]+k[2]))+k[3])/6);
x0=x0+h;
y0=y0+k[4];
printf ("\nThe value of y(%f) is = %f\n", x0, y0);
}
while (x0 < xn);
getch();
return 0;
}
CODE 01
//GAURAV MITTAL(2K9/EE/830)
//QUESTION 1. Addition of two numbers
#include
#include
int main ()
{
float a,b,c;
printf("ADDITION OF TWO NUMBERS");
printf("\nEnter first number : ");
scanf("%f",&a);
printf("\nEnter second number : ");
scanf("%f",&b);
c=a+b;
printf("\nThe sum of two numbers is : %f",c);
getch();
return 0;
}
Friday, April 15, 2011
Subscribe to:
Posts (Atom)











