SYBCom(CA) DS Practical Slip 16 Q.A

Write a C program accept the polynomial and display it in format e.g. 6x^4+2x^2+5x^1+3

#include <stdio.h>

int main() {
    int degree;

    printf("Enter the degree of the polynomial: ");
    scanf("%d", &degree);

    int coeff[degree + 1];

    // Accept coefficients
    for(int i = degree; i >= 0; i--) {
        printf("Enter coefficient for x^%d: ", i);
        scanf("%d", &coeff[i]);
    }

    // Display polynomial
    printf("Polynomial: ");
    for(int i = degree; i >= 0; i--) {
        if(coeff[i] != 0) {
            if(i != degree && coeff[i] > 0)
                printf("+");
            printf("%dx^%d", coeff[i], i);
        }
    }
    printf("\n");

    return 0;
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top