Monday, July 30, 2012

Pointers in C - a quick wrap-up

Pointers Coming soon..........




Definition:


Syntax:



Uses:



Types of pointer:




Dynamic Memory Allocation:




Examples:



Pros and Cons:


lvalue and rvalue in c

In c
lvalue are reference to memory their value persist even after execution of statement. lvalue are mainly identifier, variables which can be on both sides of '='  assignment operator.

whereas rvalue are the value(result) of the expression,it's value is persist just for a statement execution,  rvalue (value, r optional) can only be on right side of '=' assignment operator

for example


1. int x=6;


here x is lvalue and 6 is rvalue.


2. int x=x+y+z;


x- lvalue and (x+y+z) is rvalue.


3. x+5=y+z


its and error because left operand must be lvalue but given is rvalue.


4. Also contact variable are rvalue so following expression is illegal


int const x=6;
x=7;






Sunday, July 29, 2012

How to make Bootable Pendrive



Insert Pen-drive, Open command prompt and type following command one after another.


  1. diskpart
  2. list disk
  3. select disk X
  4. clean
  5. create partition primary
  6. format fs=fat32 quick
  7. active
  8. assign
  9. exit


Now copy your operating system to pendrive.

(Avoid burning  CD/DVD for  every os...... use bootable pendrive, Save Environment)

Best Websites for C

Passing Variable Arguments to Functions in C

comming soon

Friday, July 20, 2012

C Program for Simphson 1/3 rd rule





/*Simphson's 1/3rd Rule */

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

double value(double);

double **e,n;

void main()

{

double p,i,j,a,b,m,*x,*f,h,odd,even,I;

clrscr();

printf("Enter Degree of the Equation");

scanf("%lf",&n);

p=n;

e=(double**)malloc(((n+1)*2)*sizeof(double));

printf("Enter Values of coeff. in decreasing order");

for(i=0;i<=n;i++)

{

scanf("%lf",&e[i][0]);

e[i][1]=p;

p--;

}

printf("Enter values of a,b,n");

scanf("%lf %lf %lf",&a,&b,&m);

x=(double*)malloc((m+1)*sizeof(double));

f=(double*)malloc((m+1)*sizeof(double));

h=(b-a)/m;

x[0]=a;

for(i=1;i<=m;i++)

{

x[i]=x[i-1]+h;

printf(" %lf",x[i]);

}

for(i=0;i<=m;i++)

{

f[i]=value(x[i]);

printf(" %lf",f[i]);

}

odd=0;

for(i=1;i<m;i+=2)

{

odd+=f[i];

}

even=0;

for(i=2;i<m-1;i+=2)

{

even+=f[i];

}

I=((b-a)/3*n)*(f[0]+4*odd+2*even+f[m+1]);

printf("Result=%lf",I);

getch();

}

double value(double a)

{

double s=0,i;

for(i=0;i<=n;i++)

{

s=s+e[i][0]*pow(a,e[i][1]);

}

return s;

}








C Program for Langrange Interpolating Polynomial





/* L.I.P */




#include<stdio.h>

#include<conio.h>

#include<alloc.h>

float **x;

void main()

{

float n,i,j,*l,*f,R,a;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

puts("Lagrange Interpolating Polynomial");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

printf("How many Values you are Entering");

scanf("%f",&n);

puts("-------------------------------------------------------");

x=(float**)malloc(((n+1)*2)*4);

l=(float*)malloc(n*4);

printf("Enter Value of xi and f(xi) Simultaneously");

for(i=0;i<n;i++)

{

scanf("%f %f",&x[i][0],&x[i][1]);

}

puts("-------------------------------------------------------");

printf("Enter value of x");

scanf("%f",&a);

puts("-------------------------------------------------------");

for(i=0;i<n;i++)

{

l[i]=1;

for(j=0;j<n;j++)

{

if(i!=j)

l[i]*=(a-x[j][0])/(x[i][0]-x[j][0]);

}

}

R=0;

for(i=0;i<n;i++)

{

R+=l[i]*x[i][1];

}

printf("Result=%f",R);

puts("\n-------------------------------------------------------");

getch();

}




Output:-




-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Lagrange Interpolating Polynomial

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

How many Values you are Entering

3

-------------------------------------------------------

Enter Value of xi and f(xi) Simultaneously

1 0

4 1.386294

6 1.791760

-------------------------------------------------------

Enter value of x

2

-------------------------------------------------------

Result=0.565844

-------------------------------------------------------





C Program for Quadratic Interpolation





/*Quadratic Interpolation*/




#include<stdio.h>

#include<conio.h>

void main()

{

float x,b0,b1,b2,x0,x1,x2,f,f0,f1,f2;

clrscr();

puts("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Quadratic Interpolation");

puts("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

printf("Enter value of x0,f(x0)");

scanf("%f %f",&x0,&f0);

puts("----------------------------------------");

printf("Enter value of x1,f(x1)");

scanf("%f %f",&x1,&f1);

puts("----------------------------------------");

printf("Enter value of x2,f(x2)");

scanf("%f %f",&x2,&f2);

puts("----------------------------------------");

printf("Enter value of x");

scanf("%f",&x);

puts("----------------------------------------");

b0=f0;

b1=(f1-f0)/(x1-x0);

b2=(((f2-f1)/(x2-x1))-((f1-f0)/(x1-x0)))/(x2-x0);

f=b0+b1*(x-x0)+b2*(x-x0)*(x-x1);

printf("b0=%f\nb1=%f\nb2=%f\nf(x)=%f",b0,b1,b2,f);

puts("\n----------------------------------------");

getch();

}




Output:-

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Quadratic Interpolation

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Enter value of x0,f(x0)

1 0

----------------------------------------

Enter value of x1,f(x1)

4 1.5863

----------------------------------------

Enter value of x2,f(x2)

6 1.7918

----------------------------------------

Enter value of x

2

----------------------------------------

b0=0.000000

b1=0.528767

b2=-0.085203

f(x)=0.699173

----------------------------------------


C Program for Linear Interpolation



/*Linear Interpolation*/




#include<stdio.h>

#include<conio.h>

void main()

{

float x1,x0,x,f0,f1,f;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

puts("Program for Linear Interpolation");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"-);

printf("Enter the values of x0 and f(x0)\n");

scanf("%f %f",&x0,&f0);

puts("----------------------------------------");

printf("Enter the values of x1 and f(x1)");

scanf("%f %f",&x1,&f1);

puts("----------------------------------------");

printf("Enter the values of x ");

scanf("%f",&x);

puts("----------------------------------------");

f=f0+((f1-f0)*(x-x0)/(x1-x0));

printf("%f",f);

puts("\n--------------------------------------");

getch();

}

Output:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Program for Linear Interpolation

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Enter the values of x0 and f(x0)

1 0

----------------------------------------

Enter the values of x1 and f(x1)

6 1.791759

----------------------------------------

Enter the values of x

2

----------------------------------------

0.358352

--------------------------------------





C Program for Polynomial Regression








Program:-15

/* Polynmial Regresion */

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void main()

{

float *x,*y,n,i,x1=0,x12=0,y1=0,x1y1=0,a1,a0,e;

printf("How many values You are Entering");

scanf("%f",&n);

x=(float*)malloc(n*4);

y=(float*)malloc(n*4);

puts("Enter coressponding Elements X & Y");

for(i=0;i<n;i++)

{

scanf("%f %f",&x[i],&y[i]);

x1+=x[i];

y1+=y[i];

x1y1+=(x[i]*y[i]);

x12+=(x[i]*x[i]);

}

a1=(n*x1y1-x1*y1)/(n*x12-x1*x1);

a0=y1/n-a1*x1/n;

e=y[0]-a0-a1*x[0];

printf("ao=%f\na1=%f\ne%f",a0,a1,e);

}























































Output of Program:-

How many values You are Entering

6

Enter coressponding Elements X & Y

0 2.1

1 7.7

2 13.6

3 27.2

4 40.4

5 61.1

Matrix

6.000000 15.000000 55.000000 152.100006

15.000000 55.000000 225.000000 583.599976

55.000000 225.000000 979.000000 2480.800049

Required Equation

Y=4.632166+2.271759X+1.869648X2











C Program for Multiple Regression





/*Multiple Regression */

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

void main()

{

float f,k,sum=0,a[3][4],*x1,*x2,*y,n,i,j,p[]={0,0,0};

float x_1=0,x_2=0,x12=0,x22=0,x1x2=0,y1=0,x1y1=0,x2y1=0,a1,a0,e;

clrscr();

printf("Multiple Regression");

printf("\nHow many values You are Entering");

scanf("%f",&n);

x1=(float*)malloc(n*4);

x2=(float*)malloc(n*4);

y=(float*)malloc(n*4);

puts("Enter coressponding Elements X1,X2 & Y");

for(i=0;i<n;i++)

{

scanf("%f %f %f",&x1[i],&x2[i],&y[i]);

x_1+=x1[i];

x_2+=x2[i];

x1x2+=x1[i]*x2[i];

x12+=x1[i]*x1[i];

x22+=x2[i]*x2[i];

y1=y1+y[i];

x1y1=x1y1+x1[i]*y[i];

x2y1=x2y1+x2[i]*y[i];

}

a[0][0]=n;

a[0][1]=a[1][0]=x_1;

a[0][2]=a[2][0]=x_2;

a[1][2]=a[2][1]=x1x2;

a[1][1]=x12;

a[2][2]=x22;

a[0][3]=y1;

a[1][3]=x1y1;

a[2][3]=x2y1;

puts("\nMatrix");

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{

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

}

printf("\n");

}

/* Gauss Elimination */

for(k=0;k<2;k++)

{

for(i=k+1;i<3;i++)




{

for(i=k+1;i<3;i++)

{

f=a[i][k]/a[k][k];

for(j=0;j<4;j++)

}

}

for(i=2;i>=0;i--)

{

sum=a[i][3];

for(j=0;j<3;j++)

{

sum=sum-a[i][j]*p[j];

}

p[i]=sum/a[i][i];

}

e=y[0]-p[0]-p[1]*x1[0]-p[2]*x2[0];

p[0]+=e;

puts("Required Equation");

printf("Y=%f+%fX1+%fX2",p[0],p[1],p[2]);

}

Output of Program:-

Multiple Regression

How many values You are Entering

6

Enter coressponding Elements X1,X2 & Y

0 0 5

2 1 10

2.5 2 9

1 3 0

4 6 3

7 2 27

Matrix

6.000000 16.500000 14.000000 54.000000

16.500000 76.250000 48.000000 243.500000

14.000000 48.000000 54.000000 100.000000

Required Equation

Y=5.000000+4.000000X1+-3.000000X2





C Program for Gauss Sedial Method





/* Gauss Sedial Method */

#include<stdio.h>

#include<conio.h>

void main()

{

float a[3][4],i,j,k,sum,x[]={0,0,0};

clrscr();



printf("Enter coeff. in format\nax1+bx2+c3=d");

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{

scanf("%f",&a[i][j]);

}

}




for(k=0;k<2;k++)

{

for(i=0;i<3;i++)

{

sum=a[i][3];

for(j=0;j<3;j++)

{

if(i!=j)

sum=sum-a[i][j]*x[j];

}

x[i]=sum/a[i][i];

}

puts("\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

printf("\nValue After %d Iteration\n",k+1);

for(i=0;i<3;i++)

{

printf("\tx%d=%f",(int)i+1,x[i]);

}

puts("\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

}

getch();

}










Output Of Program:-

Enter coeff. in format

ax1+bx2+c3=d

3 -0.1 -0.2 7.85

0.1 7 -0.3 -19.3

0.3 -0.2 10 71.4

-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-




Value After 0 Iteration

x1=2.616667 x2=-2.794524 x3=7.005609

-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-




Value After 0 Iteration

x1=2.990556 x2=-2.499624 x3=7.000291

-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-

















C Program for Linear Regression








Program:-14

/*Linear Regression*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

void main()

{

float *x,*y,n,i,x1=0,x12=0,y1=0,x1y1=0,a1,a0,e;

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

printf("How many values You are Entering");

scanf("%f",&n);

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

x=(float*)malloc(n*4);

y=(float*)malloc(n*4);

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Enter coressponding Elements X & Y");

for(i=0;i<n;i++)

{

scanf("%f %f",&x[i],&y[i]);

x1+=x[i];

y1+=y[i];

x1y1+=(x[i]*y[i]);

x12+=(x[i]*x[i]);

}

a1=(n*x1y1-x1*y1)/(n*x12-x1*x1);

a0=y1/n-a1*x1/n;

e=y[0]-a0-a1*x[0];

a0+=e;

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

printf("\nY=%f+%fX",a0,a1);

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

}
















Output of Program:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

How many values You are Entering7

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Enter coressponding Elements X & Y

1 0.5

2 2.5

3 2

4 4

5 3.5

6 6

7 5.8

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Y=-0.371429+0.871429X

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-



































C Program for Navive Gauss Elimination





/* Naive Gauss Elimination */

#include<stdio.h>

#include<conio.h>

void main()

{

float a[3][4],i,j,k,f,sum,x[]={0,0,0};

clrscr();

printf("Enter coeff. in format\nax1+bx2+c3=d");

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{

scanf("%f",&a[i][j]);

}

}

for(k=0;k<2;k++)

{

for(i=k+1;i<3;i++)

{

f=a[i][k]/a[k][k];

for(j=0;j<4;j++)

{

a[i][j]=a[i][j]-a[k][j]*f;

}

}

}

printf("\nForward Elimination\n");

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{

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

}

printf("\n");

}

printf("Backward substituation\n");

for(i=2;i>=0;i--)

{

sum=a[i][3];

for(j=0;j<3;j++)

{

sum=sum-a[i][j]*x[j];

}

x[i]=sum/a[i][i];

}

for(i=0;i<3;i++)

{

printf("\tx%d=%f",i,x[i]);

}




getch();

}

Output Of Program:-




Enter coeff. in format

ax1+bx2+c3=d

3 -0.1 -0.2 7.85

0.1 7 -0.3 -19.3

0.3 -0.2 10 71.6




Forward Elimination

3.000000 -0.100000 -0.200000 7.850000

-0.000000 7.003334 -0.293333 -19.561666

0.000000 0.000000 10.012042 70.284286

Backward substituation

x1=3.001360 x2=-2.499163 x3=7.019975



































C Program For Secant method 2





/*Secant Method*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<alloc.h>

#include<process.h>

#define f(x) sin(x)+cos(1+x*x)-1

double deri(double);

void main()

{

double i,p,it=50;

double v,xo,x1,ea,x_1;

char ch;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

puts("------- Finding Root Using Secant Method --------");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

puts("-------------------------------------------------");

puts("Enter Value of X-1,X0\n");

scanf("%lf %lf",&x_1,&xo);

puts("------------------------------------------------");

puts("\nEnter no. of Iteration\n");

scanf("%lf",&it);

puts("------------------------------------------------");

i=0;

puts("Iteration \tXi\t\tEa");

while(1)

{

i++;

x1=xo-(f(xo)*(x_1-xo))/(f(x_1)-f(xo));

ea=(x1-xo)*100/x1;

if(ea<0) /*-- Eliminating error due to --*/

ea=ea*(-1); /*-- Negative sign of Ea --*/

if(f(x1)==0||i==it)

{

printf("\n%3d\t\t%5.6lf\t%5.4lf",(int)i,x1,ea);

puts("\n---------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%5.6lf\t%5.4lf",(int)i,x1,ea);

x_1=xo;

xo=x1;

}

getch();

}
















Output Of Program:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

------- Finding Root Using Secant Method --------

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

-------------------------------------------------

Enter Value of X-1,X0

1 3

------------------------------------------------

Enter no. of Iteration

10

------------------------------------------------

Iteration Xi Ea




1 3.509634 14.5210

2 4.057609 13.5049

3 3.964200 2.3563

4 3.399639 16.6065

5 3.489194 2.5667

6 9.558526 63.4965

7 12.883433 25.8076

8 13.638773 5.5382

9 14.151677 3.6243

10 16.520063 4.3364

--------------------------------------------






















































































C Program for Secant Method


/*-*Secant Method*-*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<alloc.h>

#include<process.h>

float value(float);

float **e,n;

void main()

{

int i,p,it=50;

float v,xo,es,s=0,x1,ea,x_1;

char ch;

clrscr();

printf("Enter Degree of the Equation\n");

scanf("%f",&n);

puts("-------------------------------------------------");

e=(float**)malloc(((n+1)*2)*sizeof(float));

printf("Enter coeff.in decreasing order of Degree\n");

for(i=0,p=n;i<=n;i++,p--)

{

scanf("%f",&e[i][0]);

e[i][1]=p;

}

puts("-------------------------------------------------");

printf("Enter Value of X-1,X0\n");

scanf("%f %f",&x_1,&xo);

puts("------------------------------------------------");

printf("What would You Like to enter Es or Sig.fig or Iteration =>E/S/I");

ch=getche();

if(ch=='E'||ch=='e')

{

printf("\nEnter value of Es\n");

scanf("%d",&es);

}

else if(ch=='s'||ch=='S')

{

printf("\nEnter number of Sig. figure\n");

scanf("%d",s);

es=0.5*pow(10,(s-2));

}

else

{

printf("\nEnter no. of Iteration\n");

scanf("%d",&it);

}

puts("------------------------------------------------");

i=0;

printf("Iteration \tXi\t\tEa");

while(1)

{

i++;

x1=xo-(value(xo)*(x_1-xo))/(value(x_1)-value(xo));

ea=(x1-xo)*100/x1;

if(ea<0)

ea=ea*(-1);

if(ea<es||value(x1)==0||i==it)

{

printf("\n%3d\t\t%5.6f\t%5.4f",i,x1,ea);

puts("\n---------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%5.6f\t%5.4f",i,x1,ea);

x_1=xo;

xo=x1;

}




}

float value(float x)

{

int i;

float s=0;

for(i=0;i<=n;i++)

{

s=s+e[i][0]*pow(x,e[i][1]);

}

return s;

}

















































Output of Program:-

Enter Degree of the Equation

3

-------------------------------------------------

Enter coeff.in decreasing order of Degree

1 -6 11 -6.1

-------------------------------------------------

Enter Value of X-1,X0

2.5 3.5

------------------------------------------------

What would You Like to enter Es or Sig.fig or Iteration =>E/S/Ii

Enter no. of Iteration

3

------------------------------------------------

Iteration Xi Ea

1 2.711111 29.0984

2 2.871090 5.5721

3 3.221925 10.8890

---------------------------------------------

















C Program for Newton Raphson Method















/*Newton Raphson Method*/




#include<stdio.h>




#include<conio.h>




#include<math.h>




#include<alloc.h>




#include<process.h>




float value(float);




float **e,n;




float deri(float);




void main()




{




int i,p,it=50;




float v,xo,es=0,s,x1,ea;




char ch;




clrscr();










printf("Enter Degree of the Equation\n");




scanf("%f",&n);




puts("--------------------------------------------------");




e=(float**)malloc(((n+1)*2)*sizeof(float));




printf("Enter coeff.in decreasing order of Degree\n");




for(i=0,p=n;i<=n;i++,p--)




{




scanf("%f",&e[i][0]);




e[i][1]=p;




}




puts("-------------------------------------------------");




printf("Enter Value of Xo\n");




scanf("%f",&xo);




puts("-------------------------------------------------");




printf("What would You Like to enter Es or Sig.fig or Iteration =>E/S/I\n");




ch=getche();




puts("\n----------------------------------------------“);




if(ch=='E'||ch=='e')




{




printf("Enter value of Es\n");




scanf("%f",&es);




}




else if(ch=='s'||ch=='S')




{




printf("Enter number of Sig. figure\n");




scanf("%f",&s);




es=0.5*pow(10,(s-2));




}




else




{




printf("Enter no. of Iteration\n");




scanf("%d",&it);




}




i=0;




puts("-------------------------------------------------");




printf("Iteration\tX\t\tEa");




while(1)




{




i++;




x1=xo-value(xo)/deri(xo);




ea=(x1-xo)*100/x1;




if(ea<0)




ea=ea*(-1);




if(ea<es||value(x1)==0||i==it)




{




printf("\n%3d\t\t%6.5f\t\t%5.3f",i,x1,ea);




puts("\n---------------------------------------------");




getch();




exit(0);




}




printf("\n%3d\t\t%6.5f\t\t%5.3f",i,x1,ea);




xo=x1;




}




float value(float x)




{




int i;




float s=0;




for(i=0;i<=n;i++)




{




s=s+e[i][0]*pow(x,e[i][1]);




}




return s;




}




float deri(float x)




{




int i;




float s=0,t,h;




for(i=0;i<=n;i++)




{




t=e[i][0]*e[i][1];




if(e[i][1]>0)




h=e[i][1]-1;




else




h=0;




s=s+t*pow(x,h);




}




return s;













Output Of Program:-













First:-




Enter Degree of the Equation




2




--------------------------------------------------




Enter coeff.in decreasing order of Degree




-1 1.8 2.5




-------------------------------------------------




Enter Value of Xo




5




-------------------------------------------------




What would You Like to enter Es or Sig.fig or Iteration =>E/S/I




e




-------------------------------------------------




Enter value of Es




0.05




-------------------------------------------------




Iteration X Ea




1 3.35366 49.091




2 2.80133 19.717




3 2.72111 2.948




4 2.71934 0.065




5 2.71934 0.000




-----------------------------------------------






===============================


Second:-

Enter Degree of the Equation

3

--------------------------------------------------

Enter coeff.in decreasing order of Degree

2 -11.7 17.7 -5

-------------------------------------------------

Enter Value of Xo

3

-------------------------------------------------

What would You Like to enter Es or Sig.fig or Iteration =>E/S/I

i

-------------------------------------------------

Enter no. of Iteration

3

-------------------------------------------------

Iteration X Ea

1 5.13333 41.558

2 4.26975 20.226

3 3.79293 12.571

-----------------------------------------------

C Program for False Position Method 2





/*False Position Method*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

#include<process.h>

#define f(x) log10(x*x*x)-0.7

double value(double);

void main()

{

double x,v,v1,v2,xr,xu,xl,xo=0,ea,et,tv;

double i,p,it=50;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Finding Root By Using False Position Method");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("-------------------------------------------");

puts("Enter value of xl and xu ");

scanf("%lf %lf ",&xl,&xu);

puts("---------------------------------------------");

puts("Enter True Value and Iterations");

scanf("%lf %lf",&tv,&it);

puts("--------------------------------------------------");

i=0;

puts("Iteration\t Xr \t\t Ea\t\tEt");

while(1)

{

i++;

v1=f(xl);

v2=f(xu);

if(v1*v2>=0)

{

puts("Invalid Value of xl and xu");

getch();

exit(0);

}

xr=xu-(v2*(xl-xu)/(v1-v2));

ea=(xr-xo)*100/xr;

et=(tv-xr)*100/tv;

if(ea<0) /*--- Eliminating Error due to ---*/

ea=ea*-1; /*--- Negative sign of Ea ---*/

v=f(xr);

if(v==0||i==it)

{

printf("\n%3d\t\t%6.6lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);

puts("\n-----------------------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%6.6lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);

if(v1*v<0)

xu=xr;

else

xl=xr;

xo=xr;

}

getch();

}

Output Of Program:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

Finding Root By Using False Position Method

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

---------------------------------------------------------------

Enter value of xl and xu

1

3

---------------------------------------------------------------

Enter True Value and Iterations

1.262803

10

---------------------------------------------------------------

Iteration Xr Ea Et

1 1.978088 100.000000 -56.642659

2 1.770376 11.732652 -40.194166

3 1.724625 2.652832 -36.571162

4 1.714334 0.600252 -35.756283

5 1.712009 0.135855 -35.572101

6 1.711482 0.030750 -35.530425

7 1.711363 0.006960 -35.520992

8 1.711336 0.001575 -35.518857

9 1.711330 0.000357 -35.518374

10 1.711329 0.000081 -35.518264

---------------------------------------------------------------


C Program for False Position Method




/*False Position Method*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

#include<process.h>

float **e,n;

float value(float);

void main()

{

float x,v,v1,v2,xr,xu,xl,xo=0,ea,es;

int i,p,it=50;

char ch;

clrscr();



printf("Enter degree\n");

scanf("%f",&n);

e=(float**)malloc(((n+1)*2)*4);

puts("------------------------------------------");

printf("Enter coeff.in decreasing order of degree\n");

for(i=0,p=n;i<=n;i++,p--)

{

scanf("%f",&e[i][0]);

e[i][1]=p;

}

puts("------------------------------------------");

printf("Enter value of xl and xu\n");

scanf("%f %f",&xl,&xu);

puts("------------------------------------------");

puts("What would you like to Enter\nEs or Number of significant fig or Iteration .=>E/S/I");

ch=getche();

if(ch=='E'||ch=='e')

{

printf("\nEnter value of Es\n");

scanf("%f",&es);

}

else if(ch=='i'||ch=='I')

{printf("Enter no. of Iteration"); scanf("%d",&it);

}

else

{

printf("\nEnter no. of sig. fig\n");

scanf("%d",&p);

es=0.5*pow(10,(2-p));

}

puts("-------------------------------------------");

i=0;

printf("Iteration\tX\t\tEa");

while(1)

{

i++; /*-- Iteration number --*/

v1=value(xl);

v2=value(xu);

if(v1*v2>=0)

{

printf("Invalid Value of xl and xu");

getch();

exit(0);

}

xr=xu-(v2*(xl-xu)/(v1-v2));

ea=(xr-xo)*100/xr;

if(ea<0)

ea=ea*-1;

v=value(xr);

if(v==0||ea<es)

{

printf("\n%3d\t\t%6.5f\t\t%5.4f",i,xr,ea);

puts("\n-----------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%6.5f\t\t%5.4f",i,xr,ea);

if(v1*v<0)

xu=xr;

else

xl=xr;

xo=xr; }

}

float value(float x)

{

int i;

float s=0;

for(i=0;i<=n;i++)

{

s=s+e[i][0]*pow(x,e[i][1]);

}

return s;

}







Output Of Program:-




First:-

Enter degree

2

------------------------------------------

Enter coeff.in decreasing order of degree

-0.4 2.2 4.7

------------------------------------------

Enter value of xl and xu

5 10

------------------------------------------

What would you like to Enter

Es or Number of significant fig.or Iteration=>E/S/I

e

Enter value of Es

1

-------------------------------------------

Iteration X Ea

1 6.50000 100.0000

2 6.97727 6.8404

3 7.10297 1.7696

4 7.13435 0.4399

-----------------------------------------------































Second:-

Enter degree

2

------------------------------------------

Enter coeff.in decreasing order of degree

-0.5 2.5 4.5

------------------------------------------

Enter value of xl and xu

5 10

------------------------------------------




What would you like to Enter

Es or Number of significant fig.or Iteration=>E/S/I

I

Enter no. of Iteration

4

-------------------------------------------

Iteration X Ea

1 5.90000 100.0000

2 6.23853 5.4265

3 6.35184 1.7838

4 6.38825 0.5700

-----------------------------------------------




















C Program for Bisection Method 2



/*Bisection Method*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

#include<process.h>

#define f(x) log10(x*x*x)-0.7

double value(double);

void main()

{

double x,v,v1,v2,xr,xu,xl,xo=0,ea,et,tv;

double i,p,it=50;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Finding Root By Using Bisection Method");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("--------------------------------------------------");

puts("Enter value of xl and xu ");

scanf("%lf %lf ",&xl,&xu);

puts("--------------------------------------------------");

puts("Enter True Value and Iterations");

scanf("%lf %lf",&tv,&it);

puts("--------------------------------------------------");

i=0;

puts("Iteration\t Xr \t\t Ea\t\tEt");

while(1)

{

i++;

v1=f(xl);

v2=f(xu);

if(v1*v2>=0)

{

puts("Invalid Value of xl and xu");

getch();

exit(0);

}

xr=(xl+xu)/2.0;

ea=(xr-xo)*100/xr;

et=(tv-xr)*100/tv;

if(ea<0)

ea=ea*-1;

v=f(xr);

if(v==0||i==it)

{

printf("\n%3d\t\t%6.5lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);

puts("\n----------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%6.5lf\t\t%5.6lf\t\t%5.6lf",(int)i,xr,ea,et);

if(v1*v<0)

xu=xr;

else

xl=xr;

xo=xr;

}

getch();

}

Output Of Program:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Finding Root By Using Bisection Method

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

---------------------------------------------------------------Enter value of xl and xu

1

3

---------------------------------------------------------------

Enter True Value and Iterations

1.262803

10

---------------------------------------------------------------

Iteration Xr Ea Et

1 2.00000 100.000000 -58.377831

2 1.50000 33.333333 -18.783373

3 1.75000 14.285714 -38.580602

4 1.62500 7.692308 -28.681988

5 1.68750 3.703704 -33.631295

6 1.71875 1.818182 -36.105948

7 1.70312 0.917431 -34.868622

8 1.71094 0.456621 -35.487285

9 1.71484 0.227790 -35.796617

10 1.71289 0.114025 -35.641951

--------------------------------------------------------------

















C Program for bisection method



/*Bisection Method*/

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<math.h>

#include<process.h>

float **e,n;

float value(float);

void main()

{

float x,v,v1,v2,xr,xu,xl,xo=0,ea,es;

int i,p,it=50;

char ch;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Finding Root By Using Bisection Method");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

printf("Enter degree\n");

scanf("%f",&n);

e=(float**)malloc(((n+1)*2)*4);

puts("------------------------------------------");

printf("Enter coeff.in decreasing order of degree\n");

for(i=0,p=n;i<=n;i++,p--)

{

scanf("%f",&e[i][0]);

e[i][1]=p;

}

puts("------------------------------------------");

printf("Enter value of xl and xu\n");

scanf("%f %f",&xl,&xu);

puts("------------------------------------------");

puts("What would you like to Enter\nEs or Number of sig fig.or Iteration=>E/S/I");

ch=getche();

if(ch=='E'||ch=='e')

{

printf("\nEnter value of Es\n");

scanf("%f",&es);

}

else if(ch=='i'||ch=='I')

{

printf("\nEnter no. of Iteration");

scanf("%d",&it);

}

else

{

printf("\nEnter no. of sig. fig\n");

scanf("%f",&p);

es=0.5*pow(10,(2-p));

}

puts("-------------------------------------------");

i=0;

puts("Iteration\t Xr \t\t Ea");

while(1)

{

i++;

v1=value(xl);

v2=value(xu);

if(v1*v2>=0)

{

printf("Invalid Value of xl and xu");

getch();

exit(0);

}

xr=(xl+xu)/2.0;

ea=(xr-xo)*100/xr;

if(ea<0)

ea=ea*-1;

v=value(xr);

if(v==0||ea<es||i==it)

{

printf("\n%3d\t\t%6.5f\t\t%5.3f",i,xr,ea);

puts("\n---------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%6.5f\t\t%5.3f",i,xr,ea);

if(v1*v<0)

xu=xr;

else

xl=xr;

xo=xr;

}

}

float value(float x)

{

int i;

float s=0;

for(i=0;i<=n;i++)

{

s=s+e[i][0]*pow(x,e[i][1]);

}

return s;

}




Output Of Program:-

First:-

Enter degree

2

------------------------------------------

Enter coeff.in decreasing order of degree

-0.4 2.2 4.7

------------------------------------------

Enter value of xl and xu

5 10

------------------------------------------

What would you like to Enter

Es or Number of sig fig.or Iteration=>E/S/I

e

Enter value of Es

1

-------------------------------------------

Iteration Xr Ea




1 7.50000 100.000

2 6.25000 20.000

3 6.87500 9.091

4 7.18750 4.348

5 7.03125 2.222

6 7.10938 1.099

7 7.14844 0.546

-----------------------------------------------































Second:-

Enter degree

5

------------------------------------------

Enter coeff.in decreasing order of degree

0.65 -9 45.5 -88 82.3 -26

------------------------------------------

Enter value of xl and xu

0.5 1

------------------------------------------

What would you like to Enter

Es or Number of sig fig.or Iteration=>E/S/I

e

Enter value of Es

10

-------------------------------------------

Iteration Xr Ea




1 0.75000 100.000

2 0.62500 20.000

3 0.56250 11.111

4 0.59375 5.263

----------------------------------------------





C Program to find e^x


/* To find e^x */

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define f0(x) pow(2.34176,x)

#define f1(x) pow(2.34176,x)

#define f2(x) pow(2.34176,x)

#define f3(x) pow(2.34176,x)

#define f4(x) pow(2.34176,x)

void main()

{

float t,x,h,v,i=0,ea=100;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("Enter value of x & h to find ln(x)");

scanf("%f %f",&x,&h);

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

puts("------------------------------------------------");

puts("Iter. ln(x+h) \tEa");

puts("------------------------------------------------");

v=f0(x);

printf("%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*f1(x);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*f2(x)/2;

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*h*f3(x)/(3*2);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*h*h*f4(x)/(4*3*2);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

puts("\n------------------------------------------------");

getch();

}

Output of Program:-




First:-




Enter value of x & h to find e^(x+h)

2 1

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

------------------------------------------------

Iter. e^(x+h) Ea

------------------------------------------------

0 5.483840 100.000000

1 10.967680 50.000000

2 13.709599 19.999998

3 14.623572 6.249997

4 14.852066 1.538464

------------------------------------------------







Second:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Enter value of x & h to find e^(x+h)

5 0.5

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

------------------------------------------------

Iter. e^(x+h) Ea

------------------------------------------------

0 70.422577 100.000000

1 105.633865 33.333332

2 114.436691 7.692311

3 115.903831 1.265826

4 116.087227 0.157981

------------------------------------------------

















C program to find sin(x)


/* To find sin(x) */

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define f0(x) sin(x)

#define f1(x) cos(x)

#define f2(x) -sin(x)

#define f3(x) -cos(x)

#define f4(x) sin(x)

void main()

{

float t,x,h,v,i=0,ea=100;

clrscr();

puts("Enter value of x & h to find sin(x)");

scanf("%f %f",&x,&h);

x=x*3.14231/180;

h=h*3.14231/180;

puts("------------------------------------------------");

puts("Iter. ln(x+h) \tEa");

puts("------------------------------------------------");

v=f0(x);

printf("%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*f1(x);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*f2(x)/2;

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*h*f3(x)/(3*2);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

t=v;

v=v+h*h*h*h*f4(x)/(4*3*2);

ea=(v-t)*100/v;

printf("\n%2.0f\t%f\t%f",i++,v,ea);

puts("\n-----------------------------------------------");

getch();

}
















Output of Program:-




Enter value of x & h to find sin(x)

44 1

------------------------------------------------

Iter. sin(x+h) Ea

------------------------------------------------

0 0.694658 100.000000

1 0.707213 1.775254

2 0.707107 -0.014962

3 0.707106 -0.000093

4 0.707106 0.000000

------------------------------------------------
























































C Program to find ln(x)





Program.no:-2.

/* To find ln(x) */

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define f0(x) log(x)

#define f1(x) 1/x

#define f2(x) -1/(x*x)

#define f3(x) 2/(x*x*x)

void main()

{

float tv,t,x,h,v,i=0,et=100,ea=100;

clrscr();

puts("Enter value of x & h to find ln(x)");

scanf("%f %f",&x,&h);

tv=f0(x+h);

puts("-------------------------------------------------");

puts("Iter. ln(x+h) \tEa\t\tEt");

puts("-------------------------------------------------");

v=f0(x);

printf("%2.0f\t%f",i++,v);

t=v;

v=v+h*f1(x);

ea=(v-t)*100/v;et=(tv-v)*100/tv;

printf("\n%2.0f\t%f\t%f\t%f",i++,v,ea,et);

t=v;

v=v+h*h*f2(x)/2;

ea=(v-t)*100/v;et=(tv-v)*100/tv;

printf("\n%2.0f\t%f\t%f\t%f",i++,v,ea,et);

t=v;

v=v+h*h*h*f3(x)/(3*2);

ea=(v-t)*100/v;et=(tv-v)*100/tv;

printf("\n%2.0f\t%f\t%f\t%f",i++,v,ea,et);

puts("\n----------------------------------------------");

getch();

}



















Output of Program:-




First:-

Enter value of x & h to find ln(x)

1 3

-------------------------------------------------------------

Iter. ln(x+h) Ea Et

-------------------------------------------------------------

0 0.000000

1 3.000000 100.000000 -116.404259

2 -1.500000 300.000000 208.202133

3 7.500000 120.000000 -441.010651

-------------------------------------------------------------




Second:-

Enter value of x & h to find ln(x)

3 1




--------------------------------------------------------------

Iter. ln(x+h) Ea Et

--------------------------------------------------------------

0 1.098612

1 1.431946 23.278353 -3.293046

2 1.376390 -4.036325 0.714442

3 1.388736 0.888986 -0.176110



---------------------------------------------------------------


C Program for Taylor Series

/*Taylor Series*/

/*ax^n+bx^(n-1)+..........*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<alloc.h>

float **m,x,n,**o,*f;

float value(float);

float sum(float);

float deri();

int fact(int);

void main()

{

int i,p;

float h,ea;

clrscr();

printf("Enter the Degree of the Equation");

scanf("%f",&n);

puts("--------------------------------------------------");

m=(float**)malloc((n+1)*2)*sizeof(float));

f=(float*)malloc(n*4);

printf("Enter the value of coefficient\n");

p=n;

for(i=0;i<=n;i++)

{

scanf("%f",&m[i][0]);

m[i][1]=p;

p--;

}

puts("--------------------------------------------------");

printf("Enter value of x\n");

scanf("%f",&x);

puts("--------------------------------------------------");

printf("Enter value of h\n");

scanf("%f",&h);

puts("-------------------------------------------------");

for(i=0;i<=n;i++)

{

if(i==0)

f[i]=value(x);

else

f[i]=deri();

printf("\nf%d(x)=%f",i,f[i]);




}

puts("\n-----------------------------------------------");

printf("\nResult=f(x+h)=%f",sum(h));

puts("\n-----------------------------------------------");

getch();

}

float sum(float h)

{

int i;

float s=0;

for(i=0;i<=n;i++)

{

s=s+pow(h,i)*f[i]/fact(i);

}

return s;

}




float value(float x)

{

int i;

float v=0;

for(i=0;i<=n;i++)

{

v=v+m[i][0]*pow(x,m[i][1]);

}

return v;

}

float deri()

{

float v=0;

int i,t;

for(i=0;i<=n;i++)

{

t=m[i][1];

m[i][1]--;

m[i][0]=m[i][0]*t;

if(m[i][1]<0)

break;

v=v+m[i][0]*pow(x,m[i][1]);

}

return v;

}

int fact(int i)

{

int j,fact=1;

for(j=i;j>0;j--)

{

fact=fact*j;

}

return fact;

}







Output of Program:-

First:-

Enter the Degree of the Equation2

------------------------------------------------------

Enter the value of coefficient

1 1 1

-------------------------------------------------------

Enter value of x

2

------------------------------------------------------

Enter value of h

1

------------------------------------------------------




f0(x)=7.000000

f1(x)=5.000000

f2(x)=2.000000

-------------------------------------------------------




Result=f(x+h)=13.000000

-------------------------------------------------------


=============================


Second:-

Enter the Degree of the Equation

3

------------------------------------------------------

Enter the value of coefficient

2.2 -5.3 6.2 2.5

-------------------------------------------------------

Enter value of x

2

------------------------------------------------------

Enter value of h

1

------------------------------------------------------




f0(x)=11.299999

f1(x)=11.400001

f2(x)=15.800001

f3(x)=13.200001

-------------------------------------------------------




Result=f(x+h)=32.800003

-------------------------------------------------------