218k views
4 votes
Find all three-digit numbers with non-zero first digit that are equal to the sum of the cubes of their digits.

User Blaneyneil
by
5.7k points

1 Answer

4 votes

Answer:

Here is code in C++.

//include header

#include <bits/stdc++.h>

using namespace std;

// main function

int main() {

// check for all three digit numbers

for(int x=100;x<=999;x++){

int num = x;

// find 1st,2nd and 3rd digit

int d1 = x%10;

int d2 = (x/10)%10;

int d3 = x/100;

// calculate sum of cube of all three digits

int sum=pow(d1, 3)+pow(d2, 3)+pow(d3,3);

// if sum is equal to number then print it

if(sum == num){

cout<<num<<endl;

}

}

return 0;

}

Step-by-step explanation:

Check all the three digit numbers, if the sum of cubes of digits is equal to the number or not.First extract all three digit and then calculate their cubes. Then sum all those three value And then check if the sum is equal to the number or not.If the sum is equal to number then print the number.

Output:

153

370

371

407

User Maddouri
by
5.9k points