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

c programs


 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, i;

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

    int coefficients[degree + 1];

    printf("Enter the coefficients of the polynomial terms from highest to lowest degree:\n");
    for (i = degree; i >= 0; i--) {
        scanf("%d", &coefficients[i]);
    }

    printf("The polynomial is: ");

    for (i = degree; i >= 0; i--) {
        if (coefficients[i] != 0) {
            if (i == degree) {
                printf("%dx^%d", coefficients[i], i);
            } else {
                if (coefficients[i] > 0) {
                    printf("+");
                }
                if (i == 0) {
                    printf("%d", coefficients[i]);
                } else if (i == 1) {
                    printf("%dx", coefficients[i]);
                } else {
                    printf("%dx^%d", coefficients[i], i);
                }
            }
        }
    }

    printf("\n");

    return 0;
}

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

You are being redirected...

10