//Gauss Seidal method
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define MAXIT 50
# define EPS .000001
void main()
{
float a[10][10],b[10],x[10],sum,x0[10];
int i,j,n,count=0;
clrscr();
printf("Enter Number of equation : ");
scanf("%d",&n);
printf("Enter Coefficients row by row \n");
printf("One row in each line\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&a[i][j]);
printf("Enter vector elements\n");
for(i=1;i<=n;i++)
{
x0[i]=x[i]=0;
scanf("%f",&b[i]);
}
printf("N\t");
for(i=1;i<=n;i++)
printf("X%d \t",i);
printf("\n");
printf("-------------------------------------------------------------------\n");
s: printf("%d\t",count+1);
for(i=1;i<=n;i++)
{
sum=0;
for(j=1;j<=n;j++)
{
if(i==j)
continue;
else
sum=sum+(a[i][j]*x[j]);
}
x[i]=(b[i]-sum)/a[i][i];
printf("%f \t",x[i]);
}
count++;
if(fabs(x[n]-x0[n])>EPS && count<MAXIT)
{
for(i=1;i<=n;i++)
x0[i]=x[i];
printf("\n");
goto s;
}
else
{
printf("\n----------------------------------------------\nSolution : \n");
for(i=1;i<=n;i++)
printf("X%d = %3.4f\t",i,x[i]);
}
getch();
}
OUTPUT
Enter Number of equation : 4
Enter Coefficients row by row
One row in each line
10 -2 -1 -1
-2 10 -1 -1
-1 -1 10 -2
-1 -1 -2 10
Enter vector elements
3 15 27 -9
N X1 X2 X3 X4
-------------------------------------------------------------------
1 0.300000 1.560000 2.886000 -0.136800
2 0.886920 1.952304 2.956562 -0.024765
3 0.983641 1.989908 2.992402 -0.004165
4 0.996805 1.998185 2.998666 -0.000768
5 0.999427 1.999675 2.999757 -0.000138
6 0.999897 1.999941 2.999956 -0.000025
7 0.999981 1.999989 2.999992 -0.000004
8 0.999997 1.999998 2.999999 -0.000001
9 0.999999 2.000000 3.000000 -0.000000
---------------------------------------------------------------------
Solution :
X1 = 1.0000 X2 = 2.0000 X3 = 3.0000 X4 = -0.0000
Hi, this is the blog for programs in different programming languages designd by me. the all programs are original and not copied from any where.
Thursday, August 19, 2010
C Program Of NA Using Euler Method
/*Euler method */
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y,xp,h,dy,i,n;
float f(float,float);
clrscr();
printf("Solution by Euler Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. \t X\t f(x,y) \t Y\n");
printf("----------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
n=y+h*f(i,y);
printf("%2d\t %2.3f\t %5.5f\t %5.6f\n",c,i,f(i,y),n);
y=n;
}
printf("----------------------------------------------------------\n");
printf("Value of y @ %f is %f",xp,n);
getch();
}
float f(float x,float y)
{
return (y-x)/(y+x);
}
OUTPUT
Solution by Euler Method
Enter initial Boundry condition x,y : 1 2
Enter Value of X at which Y is required : 2
Enter Interval ,h : .25
No. X f(x,y) Y
----------------------------------------------------------
1 1.000 0.33333 2.083333
2 1.250 0.25000 2.145833
3 1.500 0.17714 2.190119
4 1.750 0.11170 2.218045
5 2.000 0.05169 2.230968
----------------------------------------------------------
Value of y @ 2.000000 is 2.230968
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y,xp,h,dy,i,n;
float f(float,float);
clrscr();
printf("Solution by Euler Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. \t X\t f(x,y) \t Y\n");
printf("----------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
n=y+h*f(i,y);
printf("%2d\t %2.3f\t %5.5f\t %5.6f\n",c,i,f(i,y),n);
y=n;
}
printf("----------------------------------------------------------\n");
printf("Value of y @ %f is %f",xp,n);
getch();
}
float f(float x,float y)
{
return (y-x)/(y+x);
}
OUTPUT
Solution by Euler Method
Enter initial Boundry condition x,y : 1 2
Enter Value of X at which Y is required : 2
Enter Interval ,h : .25
No. X f(x,y) Y
----------------------------------------------------------
1 1.000 0.33333 2.083333
2 1.250 0.25000 2.145833
3 1.500 0.17714 2.190119
4 1.750 0.11170 2.218045
5 2.000 0.05169 2.230968
----------------------------------------------------------
Value of y @ 2.000000 is 2.230968
C Program Of NA Using Runga-Kutta Mthod Of 4th Order
/* Runge-kutta method of 4th order to solve 10*dy/dx=(x*x)+(y*y)*/
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y0,xp,h,i,y1,m1,m2,m3,m4;
float f(float,float);
clrscr();
printf("Solution by Runge kutta 2nd order Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y0);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. X\t m1 \t m2\t m3 \t m4 \t Y\n");
printf("-------------------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
m1=h*f(i,y0);
m2=h*f(i+(h/2),y0+(m1/2));
m3=h*f(i+(h/2),y0+(m2/2));
m4=h*f(i+h,y0+m3);
y1=y0+((m1+2*m2+2*m3+m4)/6);
printf("%2d %5.6f %5.6f %5.6f %5.6f %5.6f %5.6f\n",c,i,m1,m2,m3,m4,y1);
y0=y1;
}
getch();
}
float f(float x,float y)
{
return ((x*x)+(y*y))/10;
}
OUTPUT
Solution by Runge kutta 2nd order Method
Enter initial Boundry condition x,y : 0 1
Enter Value of X at which Y is required : .09
Enter Interval ,h : .03
No. X m1 m2 m3 m4 Y
-------------------------------------------------------------------------------
1 0.000000 0.003000 0.003010 0.003010 0.003021 1.003010
2 0.030000 0.003021 0.003033 0.003033 0.003047 1.006043
3 0.060000 0.003047 0.003062 0.003062 0.003079 1.009106
4 0.090000 0.003079 0.003097 0.003097 0.003117 1.012204
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y0,xp,h,i,y1,m1,m2,m3,m4;
float f(float,float);
clrscr();
printf("Solution by Runge kutta 2nd order Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y0);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. X\t m1 \t m2\t m3 \t m4 \t Y\n");
printf("-------------------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
m1=h*f(i,y0);
m2=h*f(i+(h/2),y0+(m1/2));
m3=h*f(i+(h/2),y0+(m2/2));
m4=h*f(i+h,y0+m3);
y1=y0+((m1+2*m2+2*m3+m4)/6);
printf("%2d %5.6f %5.6f %5.6f %5.6f %5.6f %5.6f\n",c,i,m1,m2,m3,m4,y1);
y0=y1;
}
getch();
}
float f(float x,float y)
{
return ((x*x)+(y*y))/10;
}
OUTPUT
Solution by Runge kutta 2nd order Method
Enter initial Boundry condition x,y : 0 1
Enter Value of X at which Y is required : .09
Enter Interval ,h : .03
No. X m1 m2 m3 m4 Y
-------------------------------------------------------------------------------
1 0.000000 0.003000 0.003010 0.003010 0.003021 1.003010
2 0.030000 0.003021 0.003033 0.003033 0.003047 1.006043
3 0.060000 0.003047 0.003062 0.003062 0.003079 1.009106
4 0.090000 0.003079 0.003097 0.003097 0.003117 1.012204
C Program Of NA Using Runga-Kutta Method
/* Runge-kutta method of 2nd order */
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y0,xp,h,i,y1,m1,m2;
float f(float,float);
clrscr();
printf("Solution by Runge kutta 2nd order Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y0);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. \t X\t m1 \t m2\t Y\n");
printf("-------------------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
m1=h*f(i,y0);
m2=h*f(i+h,y0+m1);
y1=y0+(.5*(m1+m2));
printf("%2d\t%5.6f\t%5.6f\t%5.6f\t %5.6f\n",c,i,m1,m2,y1);
y0=y1;
}
getch();
}
float f(float x,float y)
{
return (y-x);
}
OUTPUT
Solution by Runge kutta 2nd order Method
Enter initial Boundry condition x,y : 0 2
Enter Value of X at which Y is required : .1
Enter Interval ,h : .1
No. X m1 m2 Y
--------------------------------------------------------------------
1 0.000000 0.200000 0.210000 2.205000
2 0.100000 0.210500 0.221550 2.421025
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y0,xp,h,i,y1,m1,m2;
float f(float,float);
clrscr();
printf("Solution by Runge kutta 2nd order Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y0);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. \t X\t m1 \t m2\t Y\n");
printf("-------------------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
m1=h*f(i,y0);
m2=h*f(i+h,y0+m1);
y1=y0+(.5*(m1+m2));
printf("%2d\t%5.6f\t%5.6f\t%5.6f\t %5.6f\n",c,i,m1,m2,y1);
y0=y1;
}
getch();
}
float f(float x,float y)
{
return (y-x);
}
OUTPUT
Solution by Runge kutta 2nd order Method
Enter initial Boundry condition x,y : 0 2
Enter Value of X at which Y is required : .1
Enter Interval ,h : .1
No. X m1 m2 Y
--------------------------------------------------------------------
1 0.000000 0.200000 0.210000 2.205000
2 0.100000 0.210500 0.221550 2.421025
C Program Of NA Using Simpson 3/8 Rule
/*Simpson 3/8 rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int j,n=0;
float x[MAX],y[MAX],sum=0,h,res,lb,ub,i;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 3/8 rule \n");
printf ("Enter initial value : ");
scanf("%f",&lb);
printf("Enter final value : ");
scanf("%f",&ub);
printf("Enter no. of Subinterval : ");
scanf("%f",&h);
h=(ub-lb)/h;
for(i=lb;i<=ub;i=i+h)
{
x[n]=i;
y[n]=1/(1+x[n]);
n++;
}
printf("No.\t X\t\tY=1/(1+x)\n");
printf("---------------------------------------\n");
sum=y[0]+y[n-1];
for(j=0;j<n;j++)
printf("%d\t%f\t%f\n",j,x[j],y[j]);
printf("---------------------------------------\n");
for(j=1;j<n-1;j++)
{
if(j%3==0)
sum=sum+y[j]*2;
else
sum=sum+y[j]*3;
}
res=((3*h)/8)*sum;
printf("\nIntegral from %f to %f when h = %f is = %f",x[0],x[n-1],h,res);
getch();
}
OUTPUT
Solution of 1/(1+x)dx using Simpson's 3/8 rule
Enter initial value : 6
Enter final value : 15
Enter no. of Subinterval : 8
No. X Y=1/(1+x)
---------------------------------------
0 6.000000 0.142857
1 7.125000 0.123077
2 8.250000 0.108108
3 9.375000 0.096386
4 10.500000 0.086957
5 11.625000 0.079208
6 12.750000 0.072727
7 13.875000 0.067227
8 15.000000 0.062500
---------------------------------------
Integral from 6.000000 to 15.000000 when h = 1.125000 is = 0.817303
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int j,n=0;
float x[MAX],y[MAX],sum=0,h,res,lb,ub,i;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 3/8 rule \n");
printf ("Enter initial value : ");
scanf("%f",&lb);
printf("Enter final value : ");
scanf("%f",&ub);
printf("Enter no. of Subinterval : ");
scanf("%f",&h);
h=(ub-lb)/h;
for(i=lb;i<=ub;i=i+h)
{
x[n]=i;
y[n]=1/(1+x[n]);
n++;
}
printf("No.\t X\t\tY=1/(1+x)\n");
printf("---------------------------------------\n");
sum=y[0]+y[n-1];
for(j=0;j<n;j++)
printf("%d\t%f\t%f\n",j,x[j],y[j]);
printf("---------------------------------------\n");
for(j=1;j<n-1;j++)
{
if(j%3==0)
sum=sum+y[j]*2;
else
sum=sum+y[j]*3;
}
res=((3*h)/8)*sum;
printf("\nIntegral from %f to %f when h = %f is = %f",x[0],x[n-1],h,res);
getch();
}
OUTPUT
Solution of 1/(1+x)dx using Simpson's 3/8 rule
Enter initial value : 6
Enter final value : 15
Enter no. of Subinterval : 8
No. X Y=1/(1+x)
---------------------------------------
0 6.000000 0.142857
1 7.125000 0.123077
2 8.250000 0.108108
3 9.375000 0.096386
4 10.500000 0.086957
5 11.625000 0.079208
6 12.750000 0.072727
7 13.875000 0.067227
8 15.000000 0.062500
---------------------------------------
Integral from 6.000000 to 15.000000 when h = 1.125000 is = 0.817303
C Program Of NA Using Simpson 1/3 Rule
/*Simpson 1/3 Rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 1/3 rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
y[i]=1/(x[i]+1);
}
printf("X \t\t Y\n");
for(i=1;i<=n;i++)
printf("%f\t%f\n",x[i],y[i]);
h=x[2]-x[1];
printf("\nSegment Interval `h':=0.25\n");
sum=sum+y[1]+y[n];
for(i=2;i<n;i++)
{
if(i%2==0)
sum=sum+y[i]*4;
else
sum=sum+y[i]*2;
}
res=sum*(0.25/3);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT
Solution of 1/(1+x)dx using Simpson's 1/3 rule
Enter Number of data points : 5
Enter value of table value for x column
0 .25 .50 .75 1
X Y
0.000000 1.000000
0.250000 0.800000
0.500000 0.666667
0.750000 0.571429
1.000000 0.500000
Segment Interval `h':=0.25
Integral from 0.00 to 1.00 is : = 0.693254
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 1/3 rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
y[i]=1/(x[i]+1);
}
printf("X \t\t Y\n");
for(i=1;i<=n;i++)
printf("%f\t%f\n",x[i],y[i]);
h=x[2]-x[1];
printf("\nSegment Interval `h':=0.25\n");
sum=sum+y[1]+y[n];
for(i=2;i<n;i++)
{
if(i%2==0)
sum=sum+y[i]*4;
else
sum=sum+y[i]*2;
}
res=sum*(0.25/3);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT
Solution of 1/(1+x)dx using Simpson's 1/3 rule
Enter Number of data points : 5
Enter value of table value for x column
0 .25 .50 .75 1
X Y
0.000000 1.000000
0.250000 0.800000
0.500000 0.666667
0.750000 0.571429
1.000000 0.500000
Segment Interval `h':=0.25
Integral from 0.00 to 1.00 is : = 0.693254
C Program Of NA Using Trapezoidal Interpolation
/*Trapezoidal rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution using Trapezoidal rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
printf("Enter value of x[%d] and y[%d] ",i,i);
scanf("%f%f",&x[i],&y[i]);
}
h=x[2]-x[1];
printf("\nInterval :=%f\n",h);
sum=y[1]+y[n];
for(i=2;i<n;i++)
sum=sum+y[i]*2;
res=sum*(h/2);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT :-
Solution using Trapezoidal rule
Enter Number of data points : 6
Enter value of table value set by set
Enter value of x[1] and y[1] 7.47 1.93
Enter value of x[2] and y[2] 7.48 1.95
Enter value of x[3] and y[3] 7.49 1.98
Enter value of x[4] and y[4] 7.50 2.01
Enter value of x[5] and y[5] 7.51 2.03
Enter value of x[6] and y[6] 7.52 2.06
Interval :=0.010000
Integral from 7.47 to 7.52 is : = 0.099652
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution using Trapezoidal rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
printf("Enter value of x[%d] and y[%d] ",i,i);
scanf("%f%f",&x[i],&y[i]);
}
h=x[2]-x[1];
printf("\nInterval :=%f\n",h);
sum=y[1]+y[n];
for(i=2;i<n;i++)
sum=sum+y[i]*2;
res=sum*(h/2);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT :-
Solution using Trapezoidal rule
Enter Number of data points : 6
Enter value of table value set by set
Enter value of x[1] and y[1] 7.47 1.93
Enter value of x[2] and y[2] 7.48 1.95
Enter value of x[3] and y[3] 7.49 1.98
Enter value of x[4] and y[4] 7.50 2.01
Enter value of x[5] and y[5] 7.51 2.03
Enter value of x[6] and y[6] 7.52 2.06
Interval :=0.010000
Integral from 7.47 to 7.52 is : = 0.099652
C Program Of NA Using Langrange’s Interpolation
/*Lagrange’s Interpolation */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],f[MAX],lf=1,sum=0,xp;
clrscr();
printf("Lagrange's Interpolation\n");
printf("Enter No. of Data points : ");
scanf("%d",&n);
printf("Enter data points x(i) and function value f(i)\n");
printf("(One set in each line)\n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&f[i]);
printf("Enter Value of x at which Interpolation is required : ");
scanf("%f",&xp);
for(i=1;i<=n;i++)
{
lf=1;
for(j=1;j<=n;j++)
{
if(i!=j)
lf=lf*((xp-x[j])/(x[i]-x[j]));
}
sum=sum+lf*f[i];
}
printf("Interpolated Function value : \n");
printf("at x = %5.2f is %f \n",xp,sum);
getch();
}
OUTPUT
Lagrange's Interpolation
Enter No. of Data points : 4
Enter data points x(i) and function value f(i)
(One set in each line)
0 3
1 9
3 17
5 39
Enter Value of x at which Interpolation is required : 2
Interpolated Function value : at x = 2.00 is 12.700000
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],f[MAX],lf=1,sum=0,xp;
clrscr();
printf("Lagrange's Interpolation\n");
printf("Enter No. of Data points : ");
scanf("%d",&n);
printf("Enter data points x(i) and function value f(i)\n");
printf("(One set in each line)\n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&f[i]);
printf("Enter Value of x at which Interpolation is required : ");
scanf("%f",&xp);
for(i=1;i<=n;i++)
{
lf=1;
for(j=1;j<=n;j++)
{
if(i!=j)
lf=lf*((xp-x[j])/(x[i]-x[j]));
}
sum=sum+lf*f[i];
}
printf("Interpolated Function value : \n");
printf("at x = %5.2f is %f \n",xp,sum);
getch();
}
OUTPUT
Lagrange's Interpolation
Enter No. of Data points : 4
Enter data points x(i) and function value f(i)
(One set in each line)
0 3
1 9
3 17
5 39
Enter Value of x at which Interpolation is required : 2
Interpolated Function value : at x = 2.00 is 12.700000
C Program Of NA Using Divide Deference
/* Newton Divided difference formula */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],xp,u,sum=0;
printf("Newton Divided Difference formula \n");
printf("Enter No. of data points : ");
scanf("%d",&n);
printf("Enter values of X and f(x) set by set each at once \n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&fx[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
for(i=2;i<=n;i++)
for(j=1;j<=n-i+1;j++)
fx[j][i]=(fx[j+1][i-1]-fx[j][i-1])/(x[j+i-1]-x[j]);
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i<n;i++)
printf("y%d ",i);
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f\t ",x[i]);
for(j=1;j<=n-i+1;j++)
printf("%4.2f ",fx[i][j]);
printf("\n");
}
printf("\n-----------------------------------------------------------------\n");
sum=fx[1][1];
for(i=2;i<=n;i++)
{
u=1;
for(j=1;j<i;j++)
u=u*(xp-x[j]);
u=u*fx[1][i];
sum=sum+u;
}
printf("For %3.2f Interpolated value = %4.2f",xp,sum);
getch();
}
OUTPUT
Newton Divided Difference formula
Enter No. of data points : 5
Enter values of X and f(x) set by set each at once
-1 3
0 -6
3 39
6 822
7 1611
Enter value of X at which interpolation is required : 4
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4
-----------------------------------------------------------------------------
-1.00 3.00 -9.00 6.00 5.00 1.00
0.00 -6.00 15.00 41.00 13.00
3.00 39.00 261.00 132.00
6.00 822.00 789.00
7.00 1611.00
-----------------------------------------------------------------------------
For 4.00 Interpolated value = 138.00
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],xp,u,sum=0;
printf("Newton Divided Difference formula \n");
printf("Enter No. of data points : ");
scanf("%d",&n);
printf("Enter values of X and f(x) set by set each at once \n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&fx[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
for(i=2;i<=n;i++)
for(j=1;j<=n-i+1;j++)
fx[j][i]=(fx[j+1][i-1]-fx[j][i-1])/(x[j+i-1]-x[j]);
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i<n;i++)
printf("y%d ",i);
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f\t ",x[i]);
for(j=1;j<=n-i+1;j++)
printf("%4.2f ",fx[i][j]);
printf("\n");
}
printf("\n-----------------------------------------------------------------\n");
sum=fx[1][1];
for(i=2;i<=n;i++)
{
u=1;
for(j=1;j<i;j++)
u=u*(xp-x[j]);
u=u*fx[1][i];
sum=sum+u;
}
printf("For %3.2f Interpolated value = %4.2f",xp,sum);
getch();
}
OUTPUT
Newton Divided Difference formula
Enter No. of data points : 5
Enter values of X and f(x) set by set each at once
-1 3
0 -6
3 39
6 822
7 1611
Enter value of X at which interpolation is required : 4
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4
-----------------------------------------------------------------------------
-1.00 3.00 -9.00 6.00 5.00 1.00
0.00 -6.00 15.00 41.00 13.00
3.00 39.00 261.00 132.00
6.00 822.00 789.00
7.00 1611.00
-----------------------------------------------------------------------------
For 4.00 Interpolated value = 138.00
C Program Of NA Using Backward Interpolation
/* Backward Interpolation */
# include <stdio.h<
# include <conio.h<
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],u,xp,sum,p;
clrscr();
printf("\t\t\t BACKWARD INTERPOLATION\n");
printf("-------------------------------------------------------------------\n");
printf("Enter number of data points : ");
scanf("%d",&n);
// reading independant & dependant value
printf("Enter data values set by set \n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&fx[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
u=(xp-x[n])/(x[2]-x[1]);
// generating table
for(i=2;i<=n;i++)
for(j=i;j<=n;j++)
fx[j][i]=fx[j][i-1]-fx[j-1][i-1];
// printing table
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i<n;i++)
printf("y%d ",i);
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f ",x[i]);
for(j=1;j<=i;j++)
printf("%6.2f ",fx[i][j]);
printf("\n");
}
printf("-----------------------------------------------------------------\n");
// calculting interpolated value
sum=fx[n][1]+fx[n][2]*u;
for(i=2;i<n;i++)
{
p=u;
for(j=1;j<i;j++)
p=p*(u+j);
p=p/fact(i);
sum=sum+p*fx[n][i+1];
}
printf("Value of U := %5.2f\n",u);
printf("Interpolation value := %4.4f\n",sum);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
OUTPUT
BACKKWARD INTERPOLATION
------------------------------------------------------------------------------
Enter number of data points : 7
Enter data values set by set
3 13
4 21
5 31
6 43
7 57
8 73
9 91
Enter value of X at which interpolation is required : 10
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4 y5 y6
-----------------------------------------------------------------------------
3.00 13.00
4.00 21.00 8.00
5.00 31.00 10.00 2.00
6.00 43.00 12.00 2.00 0.00
7.00 57.00 14.00 2.00 0.00 0.00
8.00 73.00 16.00 2.00 0.00 0.00 0.00
9.00 91.00 18.00 2.00 0.00 0.00 0.00 0.00
------------------------------------------------------------------------------
Value of U := 1.00
Interpolation value := 111.0000
# include <stdio.h<
# include <conio.h<
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],u,xp,sum,p;
clrscr();
printf("\t\t\t BACKWARD INTERPOLATION\n");
printf("-------------------------------------------------------------------\n");
printf("Enter number of data points : ");
scanf("%d",&n);
// reading independant & dependant value
printf("Enter data values set by set \n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&fx[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
u=(xp-x[n])/(x[2]-x[1]);
// generating table
for(i=2;i<=n;i++)
for(j=i;j<=n;j++)
fx[j][i]=fx[j][i-1]-fx[j-1][i-1];
// printing table
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i<n;i++)
printf("y%d ",i);
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f ",x[i]);
for(j=1;j<=i;j++)
printf("%6.2f ",fx[i][j]);
printf("\n");
}
printf("-----------------------------------------------------------------\n");
// calculting interpolated value
sum=fx[n][1]+fx[n][2]*u;
for(i=2;i<n;i++)
{
p=u;
for(j=1;j<i;j++)
p=p*(u+j);
p=p/fact(i);
sum=sum+p*fx[n][i+1];
}
printf("Value of U := %5.2f\n",u);
printf("Interpolation value := %4.4f\n",sum);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
OUTPUT
BACKKWARD INTERPOLATION
------------------------------------------------------------------------------
Enter number of data points : 7
Enter data values set by set
3 13
4 21
5 31
6 43
7 57
8 73
9 91
Enter value of X at which interpolation is required : 10
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4 y5 y6
-----------------------------------------------------------------------------
3.00 13.00
4.00 21.00 8.00
5.00 31.00 10.00 2.00
6.00 43.00 12.00 2.00 0.00
7.00 57.00 14.00 2.00 0.00 0.00
8.00 73.00 16.00 2.00 0.00 0.00 0.00
9.00 91.00 18.00 2.00 0.00 0.00 0.00 0.00
------------------------------------------------------------------------------
Value of U := 1.00
Interpolation value := 111.0000
C Program Of NA Using Forward Interpolation
/* Forward Interpolation */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],u,xp,sum,p;
clrscr();
printf("\t\t\t FORWARD INTERPOLATION\n");
printf("-------------------------------------------------------------------\n");
printf("Enter number of data points : ");
scanf("%d",&n);
// reading independant & dependant value
printf("Enter data values set by set \n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&fx[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
u=(xp-x[1])/(x[2]-x[1]);
// generating table
for(i=2;i<=n;i++)
for(j=1;j<=n-i+1;j++)
fx[j][i]=fx[j+1][i-1]-fx[j][i-1];
// printing table
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i printf("y%d ",i);
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f ",x[i]);
for(j=1;j<=n-i+1;j++)
printf("%6.2f ",fx[i][j]);
printf("\n");
}
printf("-------------------------------------------------------------------\n");
//calculting interpolated value
sum=fx[1][1]+fx[1][2]*u;
for(i=2;i<n;i++)
{
p=u;
for(j=1;j<i;j++)
p=p*(u-j);
p=p/fact(i);
sum=sum+p*fx[1][i+1];
}
printf("Value of U := %5.2f\n",u);
printf("Interpolation value := %4.4f\n",sum);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
OUTPUT :-
FORWARD INTERPOLATION
------------------------------------------------------------------------------
Enter number of data points : 6
Enter data values set by set
.20 1.65
.22 1.66
.24 1.68
.26 1.69
.28 1.70
.30 1.71
Enter value of X at which interpolation is required : .23
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4 y5
-----------------------------------------------------------------------------
0.20 1.65 0.01 0.01 -0.02 0.03 -0.04
0.22 1.66 0.02 -0.01 0.01 -0.01
0.24 1.68 0.01 -0.00 0.00
0.26 1.69 0.01 0.00
0.28 1.70 0.01
0.30 1.71
------------------------------------------------------------------------------
Value of U := 1.50
Interpolation value := 1.6712
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],u,xp,sum,p;
clrscr();
printf("\t\t\t FORWARD INTERPOLATION\n");
printf("-------------------------------------------------------------------\n");
printf("Enter number of data points : ");
scanf("%d",&n);
// reading independant & dependant value
printf("Enter data values set by set \n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&fx[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
u=(xp-x[1])/(x[2]-x[1]);
// generating table
for(i=2;i<=n;i++)
for(j=1;j<=n-i+1;j++)
fx[j][i]=fx[j+1][i-1]-fx[j][i-1];
// printing table
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f ",x[i]);
for(j=1;j<=n-i+1;j++)
printf("%6.2f ",fx[i][j]);
printf("\n");
}
printf("-------------------------------------------------------------------\n");
//calculting interpolated value
sum=fx[1][1]+fx[1][2]*u;
for(i=2;i<n;i++)
{
p=u;
for(j=1;j<i;j++)
p=p*(u-j);
p=p/fact(i);
sum=sum+p*fx[1][i+1];
}
printf("Value of U := %5.2f\n",u);
printf("Interpolation value := %4.4f\n",sum);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
OUTPUT :-
FORWARD INTERPOLATION
------------------------------------------------------------------------------
Enter number of data points : 6
Enter data values set by set
.20 1.65
.22 1.66
.24 1.68
.26 1.69
.28 1.70
.30 1.71
Enter value of X at which interpolation is required : .23
------------------------------------------------------------------------------
X F(x) y1 y2 y3 y4 y5
-----------------------------------------------------------------------------
0.20 1.65 0.01 0.01 -0.02 0.03 -0.04
0.22 1.66 0.02 -0.01 0.01 -0.01
0.24 1.68 0.01 -0.00 0.00
0.26 1.69 0.01 0.00
0.28 1.70 0.01
0.30 1.71
------------------------------------------------------------------------------
Value of U := 1.50
Interpolation value := 1.6712
C Program Of NA Using Guass Animation
/*Gauss Elimination */
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define MAX 10
void main()
{
int i,j,n,k;
float mat[MAX][MAX],x[MAX],temp,pivot,sum=0;
clrscr();
printf("\t\t\t GAUSS ELIMINITION METHOD\n");
printf("-------------------------------------------------------------------\n");
printf("Enter No of Equtions : ");
scanf("%d",&n);
printf("Enter Coefficients of Eqution \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&mat[i][j]);
printf("Enter Constant value\n");
for(i=1;i<=n;i++)
{
scanf("%f",&mat[i][n+1]);
x[i]=mat[i][n+1];
}
for(i=2;i<=n;i++)
{
for(j=i;j<=n;j++)
{
pivot=mat[j][i-1]/mat[i-1][i-1];
for(k=i-1;k<=n+1;k++)
mat[j][k]=mat[j][k]-pivot*mat[i-1][k];
}
}
printf("Eliminated matrix as :- \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
printf("\t%.2f",mat[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
{
if(mat[i][i]==0)
{
printf("Since diagonal element become zero\n Hence solution is not possible\n");
exit(1);
}
}
printf("Solution : \n");
for(i=0;i {
sum=0;
for(j=n;j>n-i;j--)
sum=sum+mat[n-i][j];
x[n-i]=(mat[n-i][n+1]-sum*x[n])/mat[n-i][n-i];
printf("X%d = %4.2f\n",n-i,x[n-i]);
}
getch();
}
OUTPUT :-
GAUSS ELIMINITION METHOD
------------------------------------------------------------------------------
Enter No of Equtions : 3
Enter Coefficients of Eqution
4 3 -2
1 1 1
3 -2 1
Enter Constant value
5 3 2
Eliminated matrix as :-
4.00 3.00 -2.00 5.00
0.00 0.25 1.50 1.75
0.00 0.00 28.00 28.00
Solution :
X3 = 1.00
X2 = 1.00
X1 = 1.00
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define MAX 10
void main()
{
int i,j,n,k;
float mat[MAX][MAX],x[MAX],temp,pivot,sum=0;
clrscr();
printf("\t\t\t GAUSS ELIMINITION METHOD\n");
printf("-------------------------------------------------------------------\n");
printf("Enter No of Equtions : ");
scanf("%d",&n);
printf("Enter Coefficients of Eqution \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&mat[i][j]);
printf("Enter Constant value\n");
for(i=1;i<=n;i++)
{
scanf("%f",&mat[i][n+1]);
x[i]=mat[i][n+1];
}
for(i=2;i<=n;i++)
{
for(j=i;j<=n;j++)
{
pivot=mat[j][i-1]/mat[i-1][i-1];
for(k=i-1;k<=n+1;k++)
mat[j][k]=mat[j][k]-pivot*mat[i-1][k];
}
}
printf("Eliminated matrix as :- \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
printf("\t%.2f",mat[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
{
if(mat[i][i]==0)
{
printf("Since diagonal element become zero\n Hence solution is not possible\n");
exit(1);
}
}
printf("Solution : \n");
for(i=0;i
sum=0;
for(j=n;j>n-i;j--)
sum=sum+mat[n-i][j];
x[n-i]=(mat[n-i][n+1]-sum*x[n])/mat[n-i][n-i];
printf("X%d = %4.2f\n",n-i,x[n-i]);
}
getch();
}
OUTPUT :-
GAUSS ELIMINITION METHOD
------------------------------------------------------------------------------
Enter No of Equtions : 3
Enter Coefficients of Eqution
4 3 -2
1 1 1
3 -2 1
Enter Constant value
5 3 2
Eliminated matrix as :-
4.00 3.00 -2.00 5.00
0.00 0.25 1.50 1.75
0.00 0.00 28.00 28.00
Solution :
X3 = 1.00
X2 = 1.00
X1 = 1.00
C Program Of NA Using Raphson Method
/*Newton Raphson*/
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define EPS .00001
# define f(x) x*x+x-2
# define f_(x) 2*x+1
void main()
{
int n=0;
float f0,f1,x0,x1;
clrscr();
printf(" Solution of Equation x%c+x-2 through Newton raphson method",253);
printf("\nEnter Initial Guess : ");
scanf("%f",&x0);
f0=f(x0);
f1=f_(x0);
x1=(x0-(f0/f1));
printf("No\t x0\t\t f(x0)\t\t f'(x0)\t\t x1\n");
printf("-------------------------------------------------------------------\n");
while(fabs(x1-x0)/x1>EPS)
{
n++;
printf("%d\t%f\t\%f\t%f\t%f\n",n,x0,f0,f1,x1);
x0=x1;
f0=f(x0);
f1=f_(x0);
x1=(x0-(f0/f1));
}
printf("-------------------------------------------------------------------\n");
printf("Root = %f \n",x0);
printf("Function value = %f\n",f(x0));
printf("-------------------------------------------------------------------\n");
getch();
}
OUTPUT
Solution of Equation x²+x-2 through Newton raphson method
Enter Initial Guess : 0
No x0 f(x0) f'(x0) x1
--------------------------------------------------------------------------
1 0.000000 -2.000000 1.000000 2.000000
2 2.000000 4.000000 5.000000 1.200000
3 1.200000 0.640000 3.400000 1.011765
4 1.011765 0.035433 3.023530 1.000046
5 1.000046 0.000137 3.000092 1.000000
--------------------------------------------------------------------------
Root = 1.000000
Function value = 0.000000
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define EPS .00001
# define f(x) x*x+x-2
# define f_(x) 2*x+1
void main()
{
int n=0;
float f0,f1,x0,x1;
clrscr();
printf(" Solution of Equation x%c+x-2 through Newton raphson method",253);
printf("\nEnter Initial Guess : ");
scanf("%f",&x0);
f0=f(x0);
f1=f_(x0);
x1=(x0-(f0/f1));
printf("No\t x0\t\t f(x0)\t\t f'(x0)\t\t x1\n");
printf("-------------------------------------------------------------------\n");
while(fabs(x1-x0)/x1>EPS)
{
n++;
printf("%d\t%f\t\%f\t%f\t%f\n",n,x0,f0,f1,x1);
x0=x1;
f0=f(x0);
f1=f_(x0);
x1=(x0-(f0/f1));
}
printf("-------------------------------------------------------------------\n");
printf("Root = %f \n",x0);
printf("Function value = %f\n",f(x0));
printf("-------------------------------------------------------------------\n");
getch();
}
OUTPUT
Solution of Equation x²+x-2 through Newton raphson method
Enter Initial Guess : 0
No x0 f(x0) f'(x0) x1
--------------------------------------------------------------------------
1 0.000000 -2.000000 1.000000 2.000000
2 2.000000 4.000000 5.000000 1.200000
3 1.200000 0.640000 3.400000 1.011765
4 1.011765 0.035433 3.023530 1.000046
5 1.000046 0.000137 3.000092 1.000000
--------------------------------------------------------------------------
Root = 1.000000
Function value = 0.000000
C Programming - read value using scanf () and assign them to structure variables
#include<stdio.h>
#include<conio.h>
main()
{
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 b;
clrscr();
printf("\n enter book name,pages,price:");
scanf("\n %s",b.book);
scanf("\n %d",&b.pages);
scanf("\n %f",&b.price);
printf("\n bookname:%s",b.book);
printf("\n no of pages:%d",b.pages);
printf("\n book price:%f",b.price);
getch();
}
output:-enter book name,pages,price:c
500
450
book name:c
no. of pages:500
price:450
#include<conio.h>
main()
{
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 b;
clrscr();
printf("\n enter book name,pages,price:");
scanf("\n %s",b.book);
scanf("\n %d",&b.pages);
scanf("\n %f",&b.price);
printf("\n bookname:%s",b.book);
printf("\n no of pages:%d",b.pages);
printf("\n book price:%f",b.price);
getch();
}
output:-enter book name,pages,price:c
500
450
book name:c
no. of pages:500
price:450
C Programming - find the HCF & LCM of two numbers
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,r,c,d;
clrscr();
printf("\n enter two value a & b=");
scanf("\n %d%d",&a,&b);
c=a*b;
r=b%a;
while(r!=0)
{
b=a;
a=r;
r=b%a;
}
printf("\n hcf=%d",a);
d=c/a;
printf("\n lcm=%d",d);
getch();
}
output:-enter two value=12
36
hcf=12
lcm=36
#include<conio.h>
main()
{
int a,b,r,c,d;
clrscr();
printf("\n enter two value a & b=");
scanf("\n %d%d",&a,&b);
c=a*b;
r=b%a;
while(r!=0)
{
b=a;
a=r;
r=b%a;
}
printf("\n hcf=%d",a);
d=c/a;
printf("\n lcm=%d",d);
getch();
}
output:-enter two value=12
36
hcf=12
lcm=36
C Programming - check the number is prime
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,count=0;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&n);
for(i=2;i {
if(n%i==0)
count++;
}
if(count==0)
printf("\n %d is prime number",n);
else
printf("%d is not prime number",n);
getch();
}
output:-enter the number =23
23 is prime number
#include<conio.h>
main()
{
int n,i,count=0;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&n);
for(i=2;i
if(n%i==0)
count++;
}
if(count==0)
printf("\n %d is prime number",n);
else
printf("%d is not prime number",n);
getch();
}
output:-enter the number =23
23 is prime number
C Programming - calculate simpleI & compound interest
#include<stdio.h>
#include<conio.h>
main()
{
int p,t;
float r,si,ci;
clrscr();
printf("\n enter the amount,time and rate=");
scanf("\n %d%d%f",&p,&t,&r);
si=(p*r*t)/100;
printf("\n simple interest=%f",si);
ci=p+si;
printf("\n compound interest=%f",ci);
getch();
}
output:-enter the amount,time and rate=3000
4
5
simple interest =600
compound interest =3600
#include<conio.h>
main()
{
int p,t;
float r,si,ci;
clrscr();
printf("\n enter the amount,time and rate=");
scanf("\n %d%d%f",&p,&t,&r);
si=(p*r*t)/100;
printf("\n simple interest=%f",si);
ci=p+si;
printf("\n compound interest=%f",ci);
getch();
}
output:-enter the amount,time and rate=3000
4
5
simple interest =600
compound interest =3600
C Programming - display numbers in ascending and descending orders
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
printf("\n numbers is ascending order :");
for(i=1;i<=10;++i)
{
printf("%3d",i);
}
printf("\n numbers in descending order:");
for(i=10;i>0;i--)
{
printf("%3d",i);
}
getch();
}
output:-
numbers in ascending order :1 2 3 4 5 6 7 8 9 10
numbers in descending order:10 9 8 7 6 5 4 3 2 1
#include<conio.h>
main()
{
int i;
clrscr();
printf("\n numbers is ascending order :");
for(i=1;i<=10;++i)
{
printf("%3d",i);
}
printf("\n numbers in descending order:");
for(i=10;i>0;i--)
{
printf("%3d",i);
}
getch();
}
output:-
numbers in ascending order :1 2 3 4 5 6 7 8 9 10
numbers in descending order:10 9 8 7 6 5 4 3 2 1
C Programming - calculate the roots of quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,e,x1,x2,d;
clrscr();
printf("\n enter the coefficient=");
scanf("\n %f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("\n imaginary root=");
}
if(d==0)
{
printf("\n roots are equal andis x1=%fandx2=%f",x1,x2);
}
else if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
printf("\n the roots are x1=%fandx2=%f",x1,x2);
}
getch();
}
output:-enter the coefficient=1
-5
6
the root are x1=3.000000 and x2=2.000000
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,e,x1,x2,d;
clrscr();
printf("\n enter the coefficient=");
scanf("\n %f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("\n imaginary root=");
}
if(d==0)
{
printf("\n roots are equal andis x1=%fandx2=%f",x1,x2);
}
else if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
printf("\n the roots are x1=%fandx2=%f",x1,x2);
}
getch();
}
output:-enter the coefficient=1
-5
6
the root are x1=3.000000 and x2=2.000000
C Programming - calculate sum of range of number
#include<stdio.h>
#include<conio.h>
main()
{
int r1,r2,i,sum=0;
clrscr();
printf("\n enter the range of number=");
scanf("\n %d%d",&r1,&r2);
for(i=r1;i<=r2;i++)
{
sum=sum+i;
}
printf("\n sum of range of number=%d",sum);
getch();
}
Output:-
Enter the range of number=1
10
Sum of range of number=55;
#include<conio.h>
main()
{
int r1,r2,i,sum=0;
clrscr();
printf("\n enter the range of number=");
scanf("\n %d%d",&r1,&r2);
for(i=r1;i<=r2;i++)
{
sum=sum+i;
}
printf("\n sum of range of number=%d",sum);
getch();
}
Output:-
Enter the range of number=1
10
Sum of range of number=55;
C Programming - find the transpose of given matrix
#include<stdio.h>
#include<conio.h>
main()
{
int mat[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
clrscr();
printf("\n show the matrixs=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%5d",mat[i][j]);
}
printf("\n");
}
printf("\n Transpose of matrixs=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%5d",mat[j][i]);
}
printf("\n");
}
getch();
}
output:-show the matrix=1 2 3
4 5 6
7 8 9
transpose of matrix=1 4 7
2 5 8
3 6 9
#include<conio.h>
main()
{
int mat[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
clrscr();
printf("\n show the matrixs=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%5d",mat[i][j]);
}
printf("\n");
}
printf("\n Transpose of matrixs=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%5d",mat[j][i]);
}
printf("\n");
}
getch();
}
output:-show the matrix=1 2 3
4 5 6
7 8 9
transpose of matrix=1 4 7
2 5 8
3 6 9
C Programming - calculate factorial of given number
#include<stdio.h>
#include<conio.h>
void main()
{
int num,fact=1,i;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
for(i=fact;i<=num;i++)
{
fact=fact*i;
}
printf("\n factoral number=%d",fact);
getch();
}
output:-enter the number=5
factorial number=120
#include<conio.h>
void main()
{
int num,fact=1,i;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
for(i=fact;i<=num;i++)
{
fact=fact*i;
}
printf("\n factoral number=%d",fact);
getch();
}
output:-enter the number=5
factorial number=120
C Programming - convert decimal numbers into its octal
#include<stdio.h>
#include<conio.h>
main()
{
int p=1,num=0,oct=0,d;
clrscr();
printf("\n enter the decimal number=");
scanf("\n %d",&num);
while(num)
{
d=num%8;
oct=oct+p*d;
p=p*10;
num=num/8;
}
printf("\n octal number %d",oct);
getch();
}
output:-enter the decimal number=8
octal number=10
#include<conio.h>
main()
{
int p=1,num=0,oct=0,d;
clrscr();
printf("\n enter the decimal number=");
scanf("\n %d",&num);
while(num)
{
d=num%8;
oct=oct+p*d;
p=p*10;
num=num/8;
}
printf("\n octal number %d",oct);
getch();
}
output:-enter the decimal number=8
octal number=10
C Programming - check given is an Armstrong number
#include<stdio.h>
#include<conio.h>
main()
{
int num,d,sum=0,a;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
a=num;
while(a)
{
d=a%10;
sum=sum+d*d*d;
a=a/10;
}
if(num==sum)
printf("\n this is armistrong number");
else
printf("\n this is not armistrong number");
getch();
}
output:-enter the number=153
this is armistrong number
#include<conio.h>
main()
{
int num,d,sum=0,a;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
a=num;
while(a)
{
d=a%10;
sum=sum+d*d*d;
a=a/10;
}
if(num==sum)
printf("\n this is armistrong number");
else
printf("\n this is not armistrong number");
getch();
}
output:-enter the number=153
this is armistrong number
C Programming - check given number is palindrome
#include<stdio.h>
#include<conio.h>
main()
{
int num,d,a,r=0;
clrscr();
printf("\n enter the number=");
scanf("\n%d",&num);
a=num;
while(a)
{
d=a%10;
r=r*10+d;
a=a/10;
}
if(num==r)
printf("\n this is a palindrome");
else
printf("\n this is not palindrome");
getch();
}
output:- enter the number=111
this is a palindrome number
#include<conio.h>
main()
{
int num,d,a,r=0;
clrscr();
printf("\n enter the number=");
scanf("\n%d",&num);
a=num;
while(a)
{
d=a%10;
r=r*10+d;
a=a/10;
}
if(num==r)
printf("\n this is a palindrome");
else
printf("\n this is not palindrome");
getch();
}
output:- enter the number=111
this is a palindrome number
C Programming - find the reverse of given number
#include<stdio.h>
#include<conio.h>
main()
{
int num,d,rev=0;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
while(num)
{
d=num%10;
rev=rev*10+d;
num=num/10;
}
printf("\n reverse number=%d",rev);
getch();
}
output:-enter the number=654
reverse number=456
#include<conio.h>
main()
{
int num,d,rev=0;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
while(num)
{
d=num%10;
rev=rev*10+d;
num=num/10;
}
printf("\n reverse number=%d",rev);
getch();
}
output:-enter the number=654
reverse number=456
C Programming - find the greatest number among two numbers
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("\n enter the two number=");
scanf("\n %d%d",&a,&b);
if(a>b)
printf("\n greatest number=%d",a);
else
printf("\n greatest number=%d",b);
getch();
}
output:-enter the two number=12
54
greatest number=54
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("\n enter the two number=");
scanf("\n %d%d",&a,&b);
if(a>b)
printf("\n greatest number=%d",a);
else
printf("\n greatest number=%d",b);
getch();
}
output:-enter the two number=12
54
greatest number=54
Subscribe to:
Posts (Atom)