How do I get it to show the equation on the output?
Sep 29, 2010 at 4:49pm UTC
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
#include<iostream>
using namespace std;
int main()
{
//Declare variable
int n1; int n2; int n3; int n4; int sum; int prod;
//Get user input
cout<<"Enter 4 #'s" ;
cout<<endl;
cin>>n1; cin>>n2; cin>>n3; cin>>n4;
cout<<endl;
//Perform calculations
//square each #
n1*n1;
n2*n2;
n3*n3;
n4*n4;
n2-n4;
n3*(n1+n2+n4);
(n2*n3)-(n1+n4);
//Display result
//Each # squared
cout<<n1*n1<<endl;
cout<<n2*n2<<endl;
cout<<n3*n3<<endl;
cout<<n4*n4<<endl;
cout<<n2-n4<<endl;
cout<<n3*(n1+n2+n4)<<endl;
cout<<(n2*n3)-(n1+n4)<<endl;
return 0;
}
so far i've got the program itself to work the only thing i can't seem to get is how to show the actual equation in the output.
for example if i entered 3 7 4 2 the output would be:
3^2=9
7^2=49
4^2=16
2^2=4
7-2=5
4(3+7+2)=4X12=48
(7X4)-(3+2)=28-5=23
Sep 29, 2010 at 4:57pm UTC
int n1; int n2; int n3; int n4; int sum; int prod;
You don't have to repeat 'int'.
int n1, n2, n3, n4, sum, prod;
cin>>n1; cin>>n2; cin>>n3; cin>>n4;
Operator >> returns istream& so you can write
cin >> n1 >> n2 >> n3 >> n4;
1 2 3 4 5 6 7
n1*n1;
n2*n2;
n3*n3;
n4*n4;
n2-n4;
n3*(n1+n2+n4);
(n2*n3)-(n1+n4);
This code doesn't do anything. You calculate a square but you don't assign it to anything. A working example could be
int n1square = n1*n1;
As for the original problem, you can only write it yourself:
cout << n1 << "^2 = " << n1*n1 << endl;
Last edited on Sep 29, 2010 at 4:57pm UTC
Sep 29, 2010 at 5:26pm UTC
okay so far i've got
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
#include<iostream>
using namespace std;
int main()
{
//Declare variable
int n1, n2, n3, n4, prod, sum;
//Get user input
cout<<"Enter 4 #'s" ;
cout<<endl;
cin>>n1>>n2>>n3>>n4;
cout<<endl;
//Perform calculations
//square each #
int n1square = n1*n1;
int n2square = n2*n2;
int n3square = n3*n3;
int n4square = n4*n4;
n2-n4;
n3*(n1+n2+n4);
(n2*n3)-(n1+n4);
//Display result
//Each # squared
cout<<n1<<"^2=" <<n1*n1<<endl;
cout<<n2<<"^2=" <<n2*n2<<endl;
cout<<n3<<"^2=" <<n3*n3<<endl;
cout<<n4<<"^2=" <<n4*n4<<endl;
cout<<n2-n4<<endl;
cout<<n3*(n1+n2+n4)<<endl;
cout<<(n2*n3)-(n1+n4)<<endl;
return 0;
}
now how do i assign the bold lines to show on the output?
Sep 30, 2010 at 1:00pm UTC
Mate, you're missing the point.
int n1square = n1*n1;
this line takes value of n1, multiplies it with itself and puts the result in n1square. It doesn't do anything else. You never use n1square in your code. Why is it here? You could have used it on line 26, but really you don't have to.
As for printing, equation will not print itself. If you want that to see them, you have to print them. example:
1 2 3 4
int a = 1, b = 4, c = 3;
cout << a << "*x^2 + " << b << "*x + " << c << " = "
<< a << "*(x + " << (b+sqrtf(b*b-4*a*c))/2/a << ")*(x + "
<< (b-sqrtf(b*b-4*a*c))/2/a << ")" ;
Gives
1*x^2 + 4*x + 3 = 1*(x + 3)*(x + 1)
Topic archived. No new replies allowed.