How to write a C Program to Adding Two Polynomial Functions in C Programming Language ?
Solution:
/*Adding two polynomial functions*/
#include <stdio.h>
#include<stdlib.h>
struct poly* link(struct poly*);
void display(struct poly*);
struct poly* addlink(struct poly*,struct poly*, struct poly*);
struct poly
{
int coeff, pow;
struct poly *ptr;
};
int main()
{
struct poly *head1,*head2,*head3;
int num;
head1=NULL;
head2=NULL;
head3=NULL;
while(1)
{
printf("\nEnter\n1->To create first polynomial function\n2->To create second polynomial function\n3->To display first polynomial function\n4->To display second polynomial function\n5->To add both the polynomial functions\n6->To display the new polynomial created after addition\n");
scanf("%d",&num);
switch(num)
{
case 1: head1=link(head1);
break;
case 2: head2=link(head2);
break;
case 3: display(head1);
break;
case 4: display(head2);
break;
case 5: head3=addlink(head1,head2,head3);
break;
case 6: display(head3);
break;
default: printf("\nBoss ! Enter the correct value\n");
}
}
}
struct poly* link(struct poly *head1)
{
struct poly *newnode,*temp;
int value,degree;
newnode=temp=NULL;
printf("\nEnter the coefficient of the polynomial\n");
scanf("%d",&value);
printf("\nEnter the degree for variable x\n");
scanf("%d",°ree);
newnode=(struct poly*) malloc (sizeof (struct poly));
newnode->pow=degree;
newnode->coeff=value;
temp=head1;
if(temp==NULL)
{
printf("\nThe first node is created\n");
newnode->ptr=NULL;
return newnode;
}
else
{
printf("\nThe next node is being linked\n");
temp->ptr=newnode;
temp=temp->ptr;
return head1;
}
}
void display(struct poly* head)
{
if(head == NULL)
{
printf("\nThe polynomial function is not created.\nPlease create a new polynomial function.\n");
}
while(head != NULL && head->ptr != NULL)
{
printf("The polynomial function has been detected\n");
printf("%dx^%d+",head->coeff,head->pow);
head=head->ptr;
}
if(head->ptr == NULL)
{
// printf("\nEntering the last node\n");
printf("%dx^%d",head->coeff,head->pow);
head=head->ptr;
}
}
struct poly* addlink(struct poly* head,struct poly* tail,struct poly *head3 )
{
struct poly *temp1,*temp,*newnode;
temp=newnode=temp1=head3=NULL;
newnode=( struct poly*) malloc(sizeof(struct poly));
if( head == NULL || tail == NULL)
{
printf("\nYou have no node to get added\n");
}
if(head !=NULL && tail == NULL)
{
printf("\nThe existing first polynomial is the final polynomial\n");
return head;
}
if(head == NULL && tail !=NULL)
{
printf("\nThe existing second polynomial is the final polynomial\n");
return tail;
}
head3=newnode;
if(head3==NULL)
{
printf("\nEnter a node first\n");
}
if(head3 != NULL)
{
while(head !=NULL && tail !=NULL)
{
if(head->pow > tail->pow)
{
temp=tail;
temp1=head;
while((head->pow) > (temp->pow))
{
printf("\nThe element's degree in first poly. is greater than the second\n");
if(temp->ptr == NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;
}
temp=temp->ptr;
}
printf("\nYou have come to the end of the tail pointer\n");
}
if(tail->pow > head->pow)
{
temp=head;
temp1=tail;
while((tail->pow) > (temp->pow))
{
printf("\nThe element's degree in second poly. is greater than the first\n");
if(temp->ptr ==NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;
}
temp=temp->ptr;
}
printf("\nYou have come to the end of the tail pointer\n");
}
if(head->pow == tail->pow)
{
temp=head;
temp1=tail;
while((temp->pow == temp1->pow))
{
newnode->coeff = (temp->coeff + tail->coeff);
newnode->pow=temp->pow;
head3=newnode;
return head3;
temp=temp->ptr;
temp1=temp1->ptr;
}
}
}
}
}
Learn More :
Addition
Data Structure
- C program to remove last of singly linked list and insert it at beginning of list
- C Program To Read A Parenthesised Infix Expression From The User And Check Whether It Is Well Parenthesised Or Not
- C Program To Remove Last Of Singly Linked List And Insert It At Beginning of List
- C Program To Remove First Node Of Singly Linked List And Insert It At End Of The List
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
- C Program To Create Two Singly Linked List and Perform Following Operation
- C Program - Hash Table to store information about a student
- Create Two Singly Linked List Perform Differences Display It C Program
- GJK C Program Example-1
- Program to Add Two Polynomials Using Linked List C Program
- Adding Two Polynomial Functions in C Program
Structure
- C Program Structure Example-2
- C Program Structure Example
- C Program To Make Employee Payment Record Using Structure
- C Program To Store Students Record Using Structure
- Generic stack in C Program
- C Program To Multiply Two Polynomials
- C Program LEXICAL ANALYSER
- C Program Implement Binary Search Tree And Its Traversals
- GJK C Program Example-1
Two
- Write a c program to find out H.C.F. of two numbers.
- Write a C program to find maximum or equal between two numbers ?
- ADDITION OF TWO MATRICES USING C PROGRAM
- C Program To Find Product Of Two No Using MACRO
- C Program To Swap Two Numbers Without Using Third Variable
- C Program Concatenating Two Strings Into A Third System
- C Program to concatenate two strings without using string functions
- C Program to Interchanging Two Numbers
- C Program produsul scalar a doi vectori .
- C Program The dot product of two vectors
- C Program to two timing functions to measure process time
- C Program to calculate sum of two m*n matrices & store the result in 3 matrix
- C Program to swap the values of two variables by using call by reference
- Find the union and intersection of two sets of integer store it in two array C Program
- Menu driven program in C to Calculate Length, Copy into Another Compare Concatenate of Two String
- C Program To Multiply Two Polynomials
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program to Input Student Details into a File For Two Subjects
- Program to Add Two Polynomials Using Linked List C Program
- Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program
- Input Two Numbers and Print Greater Number C Program
Polynomial
Add
- C Program To Add Digits Of Entered Number
- C Program To Add n Numbers
- C program to add first seven terms of the following series using a for loop: (1/1!) + (2/2!) + (3/3!) + .....
- C Program to Adds literal to list of literals
- C Program to Add-Subtract Series 5-10+15
- C Program to Add Series 1+3+5
- C program that lets the user choose if he wants to add a item to the shopping list or print it out
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to : add/sub/multi/divi works
- Program to Add Two Polynomials Using Linked List C Program
Function
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program Character toupper() Example
- C Program Function Example
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program To Convert Temperature In Celsius To Fahrenheit, Using Function
- C Program To Find Simple Interest
- C Function to Check Vowel
- Factorial Program In C Using Function
- C Program For Prime Number Using Function
- C Function to xorSwap and addSwap in C Programming
- C Program to concatenate two strings without using string functions
- C Function to read instructions from the file and obey them
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Implements a dictionary's functionality.
- = (int*)malloc(sizeof(int)) ??
- Design a C function that shortens a string to 8 characters
- C Program to Implements a dictionary's functionality
- C Program that prompts the user to input a string, (whitespaces allowed)
- Local sounds functions in C Program
- Function Get the resource that will be gathered from the zone name
- C Program to Find the Size of File using File Handling Function
- Average function in C