Sunday 17 March 2013

C Program for Horner's Method


#include<stdio.h>
#include<conio.h>
#include<math.h>

float p[6], ply[6], q[6];

float synth(int m, float r){
                int i;
                q[0] = p[0];
                for(i=1;i<=m;i++){
                                q[i] = (q[i-1]*r)+p[i];
                }
                return(q[m]);
}

void main(){
                clrscr();
                int m,i,flag=0;
                float r, x,x1, b0, c1;
                printf("\t\tHORNER'S METHOD");
                printf("\n Enter the highest degree of the equation (max 5): ");
                scanf("%d",&m);
                                for(i=0;i<=m;i++){
                                printf("\n Coefficient x[%d] = ",m-i);
                                scanf("%f",&p[i]);
                                ply[i] = p[i];
                                }
                printf("\n Enter the initial value x0 : ");
                scanf("%f",&r);
                x = r;
                do{
                   printf("\n%f\n",x);
                   b0 = synth(m,x);

                                for(i=0;i<=5;i++){
                                                p[i]=q[i];
                                }

                   c1 = synth(m-1,x);
                   printf("\n");
                   printf("\tb0 = %f\tc1= %f",b0,c1);
                   x1 = x - (b0/c1);
                   if(fabs(x1-x) <= 0.0009){
                                flag = 1;
                   }
                   x = x1;

                                for(i=0;i<=5;i++){
                                                p[i]=ply[i];
                                }

                   getch();
                }while(flag!=1);

                printf("\nApproximate root = %f", x1);
getch();

}

Sample Output:

                          HORNER’S METHOD
Enter the highest degree of the equation (max 5): 3
Coefficient X[3] : 1
Coefficient X[2] : 9
Coefficient X[1] : 0
Coefficient X[0] : -18
Enter the initial value X0 : 1.5
1.500000
              b0 = 5.625000   c1= 33.750000
1.333333
              b0 = 0.370371   c1= 29.333332
1.328787
              b0 =  0.002071   c1= 29.005529
Approximate root = 1.320636


©Dixit Bhatta 2013

No comments:

Post a Comment

Was this post helpful? Ask any questions you have, I will try to answer them for you.