Help please

This code sums up the digits of an entered integer. How do I modify it to cube and then sum up the cubes of the digits?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>

using namespace std;

int main()
{
int num;
int sum = 0;


cout << " Enter a number : ";
cin >> num;

while ( num > 0 ) {
sum += num % 10;
num /= 10;
}

cout << "Sum = " << sum;

return 0;
}
Last edited on
Change the line that adds into the sum to cube first before adding.
sum up the cubes of the digits?


sum += (num % 10) * (num % 10) * (num % 10);

Or :
1
2
// #include <math.h>
sum += pow(num % 10, 3);
@Kikiman
Thanks! I've been trying to figure it out for so long, but something that simple never really crossed my mind...
Topic archived. No new replies allowed.