-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyEvaluation.c
65 lines (57 loc) · 1.1 KB
/
polyEvaluation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
struct poly {
int coeff;
int expo;
}p[20];
int plen, x;
void read(){
for(int i = 0; i < plen; i++){
printf("\n");
printf("Enter The Enter The Coefficient & Exponent Value Of t%d: ", i+1);
scanf("%d %d", &p[i].coeff, &p[i].expo);
}
}
void display(){
printf("\nPolynomial: ");
for(int i = 0; i < plen; i++){
if (p[i].expo == 0){
if (p[i].coeff > 0){
printf(" + %d", p[i].coeff, p[i].expo);
}
else{
printf(" +%d", p[i].coeff, p[i].expo);
}
}
else if (p[i].coeff > 0){
printf(" + %dx^%d", p[i].coeff, p[i].expo);
}
else if (p[i].coeff < 0){
printf(" + %dx^%d", p[i].coeff, p[i].expo);
}
}
}
void eval(){
int result = 0;
for(int i = 0; i < plen; i++){
if (p[i].expo == 0){
result += p[i].coeff;
}
else{
int pow = 1;
for(int _ = 0; _ < p[i].expo; _++){
pow = pow * x;
}
result += (p[i].coeff * pow);
}
}
printf("\nResult = %d\n", result);
}
void main(){
printf("\nEnter The Number Of Terms In The Polynomial: ");
scanf("%d", &plen);
read();
printf("\nEnter The Value Of x: ");
scanf("%d", &x);
display();
eval();
}