Create and Display a polynomial | C Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int coef;
int exp;
struct node *next;
};
struct node *start=NULL;
main()
{
int i,coef,exp,choice=1; // term=Number of terms in polynomial
struct node *n,*temp;
//creation of polynomial
while(choice!=0)
{
n=malloc(sizeof(struct node));
if(n==NULL)
{
printf("memory overflow");
getch();
exit(0);
}
else
{
printf("\n\n\tEnter coefficient and exponent respectively for polynomial: ");
scanf("%d%d",&coef,&exp);
n->coef=coef;
n->exp=exp;
n->next=NULL;
if(start==NULL)
{
start=n;
temp=n;
}
else
{
temp->next=n;
temp=n;
}
printf("Continue adding more terms to the polynomial list?(Yes = 1/No = 0): ");
scanf("%d",&choice);
}
}
// for display
if(start==NULL)
printf("\n\t** No Polynomial Exist **");
else
{
temp=start;
printf("\n\tPolynomial Expression:\n\t\t\tP(x) = ");
while(temp!=NULL)
{
if(temp->coef>0)
printf("+");
printf("%dx^%d",temp->coef,temp->exp);
temp=temp->next;
}
}
getch();
}
OUTPUT:
0 Comments
Please do not enter any spam link in the comment box.