C Prog
C Prog
CSIT
C-Programming
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
#include<stdlib.h> float f ( float x)
float f ( float x) {
{ float y;
float y; y= pow(x, 2)+x -2;
y= pow(x, 2)+x -2; return y;
return y; }
} void main()
void main() {
{ float x1, x2, x0, error=0.0001;
float x1, x2, x0, error=0.0001; int i=0;
int i=0; printf("\nEnter two initial guess:");
printf("\nEnter two initial guess:"); scanf("%f%f", &x1, &x2);
scanf("%f%f", &x1, &x2); do
if (f(x1 )*f(x2 )>0) {
{ x0=x1-(f(x1)*(x2-x1))/(f(x2)-f(x1));
printf("\nWrong Input!!"); x2=x1;
exit(0); x1=x0;
} i++;
else }while(fabs (f(x0))>error);
{ printf("\nRoot=%f", x0);
do printf("\nNumber of iteration=%d",i);
{ getch();
x0=(x1+x2)/2;
if(f(x0 )*f(x1 )>0) }
x1=x0;
else
x2=x0;
i++;
}while(fabs (f(x0))>error);
}
printf("\nRoot=%f", x0);
printf("\nNumber of iteration=%d",i);
getch();
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float f( float x) float g ( float x)
{ {
float y; float y;
y= pow(x, 2)+x -2; y= 2.0-x*x;
return y; return y;
} }
float fd( float x) int main()
{ {
float y; float x0, x, error, E=0.00001;
y= 2*x+1; printf("Input initial estimate of a root:\n");
return y; scanf("%f", &x0);
} while(1)
void main() {
{ x=g(x0);
float x0,x1,error=0.0001; error=(x-x0)/x;
int i=0; if(fabs(error)<E)
printf("\nGuess initial root:"); {
scanf("%f", &x1); printf("\nRoot=%f", x0);
do break;
{ }
x0=x1-(f(x1)/fd(x1)); x0=x;
x1=x0; }
i++; getch();
}while(fabs (f(x0))>error); return 0;
printf("\nRoot=%f", x0);
printf("\nNumber of iteration=%d",i); }
getch();
}
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
int main() #include<math.h>
{ #define error 0.001
float x[10], f[10], y, sum=0.0, l; int main()
int n, i, j; {
printf("\nInput number of data:"); int i, n;
scanf("%d", &n); float x[10], y[10], sumx=0.0,sumy=0.0;
printf("\nInput data points x(i) & f(i):\n"); float sumxx=0.0, sumxy=0.0;
for(i=0;i<n;i++) float meanx, meany, denom, a, b;
{ printf("how many element?:");
printf("x[%d]=",i); scanf("%d", &n);
scanf("%f", &x[i]); for(i=0;i<n;i++)
printf("f[%d]=",i); {
scanf("%f", &f[i]); printf("x[%d]=",i);
} scanf("%f",&x[i]);
printf("\nFunctional value:"); printf("y[%d]=",i);
scanf("%f", &y); scanf("%f",&y[i]);
for(i=0;i<n;i++) }
{ for(i=0;i<n;i++)
l=1; {
for(j=0;j<n;j++) sumx+=x[i];
{ sumy+=y[i];
if(j!=i) sumxx+=x[i]*x[i];
{ sumxy+=x[i]*y[i];
l=l*(y-x[j])/(x[i]-x[j]); }
} meanx=sumx/n;
} meany=sumy/n;
sum=sum+l*f[i]; denom=n*sumxx-sumx*sumx;
} if(fabs(denom)>error)
printf("\nValue at %f=%f", y, sum); {
getch(); b=(n*sumxy-sumx*sumy)/denom;
return 0; a=meany-b*meanx;
} printf("y=%fx+%f",b,a);
}
else
{
printf("\nNo Solution");
}
getch();
return 0;
}
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float f(float x) float f(float x)
{ {
return (1-exp(-x/2.0)); return (1-exp(-x/2.0));
} }
void main() void main()
{ {
float a, b, h, x, sum=0; float a, b, h, x,ans,sum=0;
int n; int n,i;
printf("Enter initial and final value of printf("Enter initial and final value of
x:\n"); x:\n");
scanf("%f%f", &a, &b); scanf("%f%f", &a, &b);
printf("\nNumber of segments:"); printf("\nNumber of segments:");
scanf("%d", &n); scanf("%d", &n);
h=(b-a)/n; h=(b-a)/n;
for(x=a;x<=b;x=x+h) for(i=1;i<n;i++)
{ {
if(x==a) x=a+i*h;
sum=sum+f(x); if(i%2==0)
else if(x==b) {
sum=sum+f(x); sum=sum+2*f(x);
else }
sum=sum+2*f(x); else{
} sum=sum+4*f(x);
sum=sum*h/2; }
printf("\nIntegral value of f(x)=%f ", sum); }
getch(); ans=(h/3)*(f(a)+f(b)+sum);
} printf("\nIntegral value of f(x)=%f ", ans);
getch();
}
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float f(float x) float fun( float x, float y)
{ {
return (1-exp(-x/2.0)); float f;
} f = x*y;
void main() return f;
{ }
float a, b, h, x,ans,sum=0; int main()
int n,i; {
printf("Enter initial and final value of int i, n;
x:\n"); float x0, y0, xp, h, y;
scanf("%f%f", &a, &b); printf("Enter initial value of x and y:");
printf("\nNumber of segments:"); scanf("%f%f",&x0,&y0);
scanf("%d", &n); printf("Enter x at which y is required:");
h=(b-a)/n; scanf("%f",&xp);
for(i=1;i<n;i++) printf("Enter step-size,h:");
{ scanf("%f",&h);
x=a+i*h; n=(xp - x0)/h;
if(i%3==0) for(i=0; i < n; i++)
{ {
sum=sum+2*f(x); y=y0+h*fun(x0,y0);
} x0=x0+h;
else{ y0=y;
sum=sum+3*f(x); printf("%f\t%f\n",x0,y);
} }
} printf("\nValue of y at x=%f id %f",x0,y0);
ans=(3*h/8)*(f(a)+f(b)+sum); getch();
printf("\nIntegral value of f(x)=%f ", ans); }
getch();
}
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
float func(float x, float y) float func(float x, float y)
{ {
float f; float f;
f=2.0*y/x;; f=2.0*y/x;;
return f; return f;
} }
int main() int main()
{ {
int i, n; int i,n;
float x0, y0, xp, h, m1, m2; float x0, y0, xp, h, m1, m2, m3, m4;
printf("Enter initial value of x and y:"); printf("Enter initial value of x and y:");
scanf("%f %f", &x0,&y0); scanf("%f %f", &x0,&y0);
printf("Enter x at which y is required:"); printf("Enter x at which y is required:");
scanf("%f", &xp); scanf("%f", &xp);
printf("Enter stepsize,h: "); printf("Enter stepsize,h: ");
scanf("%f", &h); scanf("%f", &h);
n = (xp - x0)/h; n = (xp - x0)/h;
for(i=1; i<=n; i++) for(i=1; i<=n; i++)
{ {
m1 = func(x0,y0); m1 = func(x0,y0);
m2 = func(x0+h, y0+m1*h); m2 = func(x0+0.5*h, y0+0.5*m1*h);
x0 = x0+h; m3 = func(x0+0.5*h, y0+0.5*m2*h);
y0 = y0+0.5*h*(m1+m2); m4 = func(x0+h, y0+m3*h);
printf("%f \t%f\n", x0,y0); x0 = x0+h;
} y0 = y0+(m1+2*m2+2*m3+m4)*h/6;
printf("\nValue of y at x=%f is %f",x0,y0); printf("%f\t%f\n", x0, y0);
getch(); }
return 0; printf("\nValue of y at x=%f is %f",x0, y0);
} getch();
return 0;
}
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<math.h> #include<math.h>
#define f1(x,y,z) (15-y-z)/10 #define f1(x,y,z) (15-y-z)/10
#define f2(x,y,z) (24-x-z)/10 #define f2(x,y,z) (24-x-z)/10
#define f3(x,y,z) (33-x-y)/10 #define f3(x,y,z) (33-x-y)/10
/* Error */ /* Error */
e1 = fabs(x0-x1); e1 = fabs(x0-x1);
e2 = fabs(y0-y1); e2 = fabs(y0-y1);
e3 = fabs(z0-z1); e3 = fabs(z0-z1);
i++; i++;
/* Set value for next iteration */ /* Set value for next iteration */
x0 = x1; x0 = x1;
y0 = y1; y0 = y1;
z0 = z1; z0 = z1;
}while(e1>e && e2>e && e3>e); }while(e1>e && e2>e && e3>e);
https://collegenote.pythonanywhere.com/