Home > Back-end >  How do I find the first rational zero of a polynomial in C?
How do I find the first rational zero of a polynomial in C?

Time:12-04

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:

  1. a is fixed to 1
  2. Once the first rational zero is found, the integer will be displayed and program will be stopped.
  3. If no rational zero is found, a message "No rational zero found!" will be displayed.

This is an example of the output:

This is the 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;   
}

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related