#include //stdio.h #include //conio.h #include //math.h #include //stdlib.h #define maxy 10
float power(float value,int pwr) { int i; float t=1; for(i=0;i { t = t*value; } return t; } float eval(float fun[],float val,int deg) { float t=0;int i=0; for(i=0;i<=deg;i++) { t = t + fun[i] * power(val,i); } return t; }
void main() { float a,b,c; int deg,i; float func[maxy]={0}; clrscr(); printf("\nEnter the Degree of the equation:"); scanf("%d",°); printf("\nEnter the coefficients of the equation:\n"); for(i=deg;i>=0;i--) { printf(" X^%d = ",i); scanf("%f",&func[i]); } for(i=-50;i<50;i++) { a = eval(func,i,deg); b = eval(func,i+1,deg); if((int)(b*100000)==0) { printf("\n\nThe solution is: %d",i+1); getch(); exit(0); } if((int)(a*b*1000000)<0) { break; } } for(i=0;i<100;i++) { float t = c; c = (a*eval(func,b,deg) - b*eval(func,a,deg))/(eval(func,b,deg)-eval(func,a,deg)); if((int)(c*100000)>0) { a = min(a,b); b = c; } else { a = max(a,b); b = c; } if((int)(t*100000)==(int)(c*100000)) { printf("\n\nThe solution is %f",c); getch(); exit(0); } } getch(); }
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|