-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp15polynominal.cpp
More file actions
78 lines (68 loc) · 1.98 KB
/
p15polynominal.cpp
File metadata and controls
78 lines (68 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
14).Implement a Program to find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in POLYSUM(x,y,z)
*/
#include <iostream>
using namespace std;
// Define the Polynomial structure
struct Polynomial
{
int coeff[8]; // Array to hold the coefficients for x, y, z, xy, xz, yz, xyz
};
// Function to input polynomial coefficients
void inputPolynomial(Polynomial &poly)
{
cout << "Enter the coefficients for the polynomial:\n";
cout << "a0 (constant term): ";
cin >> poly.coeff[0];
cout << "a1 (x): ";
cin >> poly.coeff[1];
cout << "a2 (y): ";
cin >> poly.coeff[2];
cout << "a3 (z): ";
cin >> poly.coeff[3];
cout << "a4 (xy): ";
cin >> poly.coeff[4];
cout << "a5 (xz): ";
cin >> poly.coeff[5];
cout << "a6 (yz): ";
cin >> poly.coeff[6];
cout << "a7 (xyz): ";
cin >> poly.coeff[7];
}
// Function to add two polynomials and store the result in POLYSUM
void addPolynomials(Polynomial &poly1, Polynomial &poly2, Polynomial &polySum)
{
for (int i = 0; i < 8; i++)
{
polySum.coeff[i] = poly1.coeff[i] + poly2.coeff[i];
}
}
// Function to display the polynomial
void displayPolynomial(const Polynomial &poly)
{
cout << "Polynomial: ";
cout << poly.coeff[0] << " + "
<< poly.coeff[1] << "x + "
<< poly.coeff[2] << "y + "
<< poly.coeff[3] << "z + "
<< poly.coeff[4] << "xy + "
<< poly.coeff[5] << "xz + "
<< poly.coeff[6] << "yz + "
<< poly.coeff[7] << "xyz\n";
}
int main()
{
cout << "ABHISHEK SINGH 2315272/2435222";
Polynomial poly1, poly2, polySum;
// Input polynomials
cout << "Enter the first polynomial:\n";
inputPolynomial(poly1);
cout << "Enter the second polynomial:\n";
inputPolynomial(poly2);
// Add the polynomials
addPolynomials(poly1, poly2, polySum);
// Display the sum of polynomials
cout << "\nThe sum of the two polynomials is:\n";
displayPolynomial(polySum);
return 0;
}