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. }

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

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

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


👉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

Another posts