writing expressions in c++

closed account (2EURX9L8)


How would the following mathematical expression be written in C++? (x is a volume)
Note: the lab manual uses single character variable names like b, a, c, and y. It is better in programming to use meaningful variable names. So when writing the formula below in C++ be sure to chose a meaningful name to use instead of x.
2x + 3^6

I know this is basic basic stuff but I am not sure how to go about it....this is what I have but I am pretty sure its wrong..

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double result;
result=2*volume+pow(3,6);
return 0;
}

That looks right (except volume needs to be defined.) You just need some input and output (io:)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> //cout, cin, end
#include <cmath> //pow(int, int)

using namespace std; //avoid prefixing all the identifiers found in namespace std

int main() {
    double volume, result; //declare volume and result
    cout << "volume = "; //print the question to standard output
    cin >> volume; //get volume's value from standard input
    result = 2 * volume + pow(3, 6); //calculate the result, and store it
    cout << "result = " << result << endl; //print the result and a line break
    return 0; //we're done, and the program ran successfully
}

(Remember to put your code in [ code ] and [/code] tags so it looks like this; or, you can highlight it and click the <> symbol to the right of the edit box.)
Last edited on
Topic archived. No new replies allowed.