Reputation: 13
I'm a newbie to the C programming and I would like to ask how do I make a program in C that would find the first rational zero of a polynomial ax3 + bx2 + cx + d with a few conditions:
This is an example of the output:
I've done some of the codes by following the example output but I couldn't figure out the "Find the first rational zero" part. These are my codes:
#include<stdio.h>
int main()
{
int a = 1, b, c, d;
int i;
int x;
printf("\nInsert value of b: ");
scanf("%d", &b);
printf("\nInsert value of c: ");
scanf("%d", &c);
printf("\nInsert value of d: ");
scanf("%d", &d);
printf("\nThe polynomial is %dx^3 + %dx^2 + %dx + %d", a, b, c, d);
printf("\n-------------------------------------------");
printf("\nPossible rational zeros are: ");
for (i=1; i <= d; ++i)
{
if (d % i == 0)
printf("\n%d\n%d", i, -i);
}
return 0;
}
Upvotes: 0
Views: 315
Reputation: 7749
The implementation you show already lists the possible rational roots using a specialization of the Rational root theorem where a is fixed to 1.
Hence what you need to do is to check for each possibility i
and -i
if it is indeed a root of the polynomial, i.e. evaluate the polynomial for x=i
and x=-i
and see if the result is 0.
Upvotes: 4