pvjc22/du3/program.c

61 lines
1.3 KiB
C
Raw Normal View History

2022-03-17 10:59:47 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2022-03-18 11:45:58 +00:00
//#include<conio.h>
2022-03-17 10:59:47 +00:00
2022-03-18 11:45:58 +00:00
/*int main()
2022-03-17 10:59:47 +00:00
{
float a[100],sum=0,x;
int n,i;
2022-03-17 11:02:36 +00:00
printf("\nEnter the value of X :: ");
scanf("%f",&x);
2022-03-18 11:45:58 +00:00
// printf("\nEnter degree of the polynomial X :: ");
//scanf("%d",&n);
2022-03-17 10:59:47 +00:00
printf("\nEnter coefficient's of the polynomial X :: \n");
for(i=n;i>=0;i--)
{
printf("\nEnter Coefficient of [ X^%d ] :: ",i);
scanf("%f",&a[i]);
}
for(i=n;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("\nValue of the polynomial is = [ %f ]\n",sum);
return 0;
2022-03-18 11:45:58 +00:00
}
*/
void main() {
int degree,x;
int coeff[10], i;
int sum = 0;
// Getting the degreee of the polynomial
printf("\nEnter the degree of the polynomial: ");
scanf("%d",&degree);
// Getting the coefficients of all terms of the polynomial
printf("\nEnter the coeffients from lower order to higher order: \n");
for(i=0; i<=degree; i++) {
scanf("%d",&coeff[i]);
}
// Getting the value of the variable
printf("\nEnter the value of x: ");
scanf("%d",&x);
// Applying Horner's rule
sum = coeff[degree];
for(i=degree-1; i>=0; i--) {
sum = sum*x + coeff[i];
}
// Printing result
printf("\nResult: %d",sum);
2022-03-17 10:59:47 +00:00
}