Skip to main content

Addition Of Two Polynomials

Addition Of Two Polynomials.


CODE
👇
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node
  4. {
  5. int exp, coef;
  6. struct node *next;
  7. }P;

  8. void create(P *p1);
  9. void display(P *p1);
  10. void add(P *p1, P *p2, P *p3);

  11. void main()
  12. {
  13. P *p1, *p2, *p3, *p4;
  14. p1=(P*)malloc(sizeof(P));
  15. p1->next=NULL;

  16. p2=(P*)malloc(sizeof(P));
  17. p2->next=NULL;

  18. p3=(P*)malloc(sizeof(P));
  19. p3->next=NULL;

  20. p4=(P*)malloc(sizeof(P));
  21. p4->next=NULL;

  22. create(p1);
  23. printf("1st Polynomial is:\t");
  24. display(p1);
  25. printf("\n\n");

  26. create(p2);
  27. printf("2nd Polynomial is:\t");
  28. display(p2);
  29. printf("\n\n");

  30. add(p1, p2, p3);
  31. printf("\n Addition of 1st and 2nd Polynomial is:\n");
  32. display(p1);
  33. printf("+");
  34. display(p2);
  35. printf("=");
  36. display(p3);
  37. printf("\n\n");
  38. }

  39. void create(P *p1)
  40. {
  41. P *temp=p1, *nn;
  42. int i, n;
  43. printf("How many terms you want to enter:");
  44. scanf("%d", &n);
  45. printf(">>>Enter terms in desending order of power<<<\n");
  46. for(i=0; i<n; i++)
  47. {
  48. nn=(P*)malloc(sizeof(P));
  49. nn->next=NULL;
  50. printf("Enter coefficient of the term: %d=", i+1);
  51. scanf("%d", &nn->coef);
  52. printf("Enter exponetial of the term:%d=", i+1);
  53. scanf("%d", &nn->exp);

  54. if(p1==NULL)
  55. p1=temp=nn;
  56. temp->next=nn;
  57. temp=nn;
  58. }

  59. void display(P *p1)
  60. {
  61. P *temp;
  62. for(temp=p1->next;temp!=NULL; temp=temp->next)
  63. printf("[%d^%d]+",temp->coef, temp->exp);
  64. printf("\b");
  65. }

  66. void add(P *p1, P *p2, P *p3)
  67. {
  68. P *t1=p1->next, *t2=p2->next, *t3=p3, *nn;
  69. int i;
  70. while(t1 && t2)
  71. {
  72. nn=(P*)malloc(sizeof(P));
  73. nn->next=NULL;
  74. if(t1->exp > t2->exp)
  75. {
  76. nn->exp=t1->exp;
  77. nn->coef=t1->coef;
  78. t1=t1->next;
  79. }
  80. else 
  81. if(t1->exp < t2->exp)
  82. {
  83. nn->exp=t2->exp;
  84. nn->coef=t2->coef;
  85. t2=t2->next;
  86. }
  87. else
  88. if(t1->exp==t2->exp)
  89. {
  90. nn->exp=t2->exp;
  91. nn->coef=t1->coef+t2->coef;
  92. t1=t1->next;
  93. t2=t2->next;
  94. }
  95. t3->next=nn;
  96. t3=nn;
  97. }
  98. while(t1)
  99. {
  100. nn=(P*)malloc(sizeof(P));
  101. nn->next=NULL;
  102. nn->exp=t1->exp;
  103. nn->coef=t1->coef;
  104. t3->next=nn;
  105. t3=nn;
  106. t1=t1->next;
  107. }

  108. while(t2)
  109. {
  110. nn=(P*)malloc(sizeof(P));
  111. nn->next=NULL;
  112. nn->next=NULL;
  113. nn->exp=t2->exp;
  114. nn->coef=t2->coef;
  115. t3->next=nn;
  116. t3=nn;
  117. t2=t2->next;
  118. }
  119. }


👉Execute👈

//Output
/*
How many terms you want to enter:2
>>>Enter terms in desending order of power<<<
Enter coefficient of the term: 1=4
Enter exponetial of the term:1=3
Enter coefficient of the term: 2=2
Enter exponetial of the term:2=3
1st Polynomial is: [4^3]+[2^3]+

How many terms you want to enter:2
>>>Enter terms in desending order of power<<<
Enter coefficient of the term: 1=5
Enter exponetial of the term:1=3
Enter coefficient of the term: 2=6
Enter exponetial of the term:2=3
2nd Polynomial is: [5^3]+[6^3]+


 Addition of 1st and 2nd Polynomial is:
[4^3]+[2^3]+[5^3]+[6^3]+=[9^3]+[8^3]+

*/



                                                                                 //ThE ProFessoR

Comments