HELP PLEASE

C++ program have the user enter a two digit integer. calculate and display the square, raised to the power of 3, square root, and the sum of the digits of the user input.

Output should look as follows

Enter an integer 56
The square of 56 is 3136
56 raised to the 3rd power is 175616
the square root of 56 is 7.48
the sum of the digits in 56 is 11
(im stuck on how to write the sum of the digits part)
(THIS IS WHAT I HAVE SO FAR)

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
float num1,sum, num2;


cout << "Enter a two digit integer: ";
cin >> num1, num2;

cout << "The square is: ";
cout << num1 * num1<<endl;



cout << "The 3rd power is: ";
cout << pow(num1, 3)<< endl;


cout << setprecision(3);
cout << "The square root is: ";
cout << sqrt(num1)<< endl;



cout << "The sum is: ";




return 0;

}


Last edited on
cin >> num1, num2;


This does not do what you think. Don't try to use the comma operator this way. Pretty much the only thing it's good for is for function parameter lists, and array initialization lists. If you try to use it anywhere else you're probably using it incorrectly.

Furthermore, if the user is supposed to entering an integer, you should receive it as an integer (int), not as two floats.

Lastly, you can extract "digits" of an integet by dividing (/) and modding (%) by 10. Note that this won't work with floats -- the variable has to be an integer.
How would i be able to identify the equation to be able to get the sum of the integer that is typed in the keyboard
To sum the digits together you need to isolate each digit. You can do this with division (/) and modding (%).

For example, given the number 56, you can "extract" the 5 by dividing by 10:

56 / 10 = 5

Modding (%) has a similar effect, but gives you the remainder after division, rather than the result of the division.


EDIT: but again -- only works with integers. This won't work if you have a float.
Last edited on
Topic archived. No new replies allowed.