### Coding Problem

If/Else Homework
A “magic” number is an integer in which the number is equal to the sum of the cubes of its digits. Write a program to determine whether a three digit number entered by the user is a magic number.

I have started on this but I do not know how to continue! Please give me hints or tips!
 12345678910 #include #include using namespace std; int main () { int x,y,z; cout<<"Input the first digit of the 3 digit number: " ; cin>>x; cout<<"Input the second digit of the 3 digit number: "; cin>>y; cout<<"Input the third digit of the 3 digit number: ";
 12345678910111213141516171819202122232425262728293031323334353637 // MagicNumbers.cpp - zoID // #include #include using namespace std; void calcMagic(int x, int y, int z) { int xT = x * x * x; // Assign cubed int yT = y * y * y; int zT = z * z * z; int sum = xT + yT + zT; // Add cubes for magic number int num = 0; // Append userinput into single integer. if (sum == num) cout << "You have magic number!" << endl; else cout << "This is NOT a magic number!" << endl; } int main () { int x,y,z; cout << "Determine Sum of Cubes is magic number" << endl; while (1) { cout << endl; // Input 3 sides cout<<"Input the first digit of the 3 digit number: "; cin>>x; cout<<"Input the second digit of the 3 digit number: "; cin>>y; cout<<"Input the third digit of the 3 digit number: "; cin>>z; calcMagic(x,y,z); } return 0; }

If I'm right in my assumption that the magic number referred to in your question is also called the narcissistic number then I have determined the magic number algorithm, however, I'm new to C++ - For the above code to work you will need to append x to y to z to get variable num and the initial user entered number (I think this is done by multiplying by the power of ten..) and then match that to sum and if it matches, voila magical number!

I guess your homework is to determine where the problem is, and fix it.

EDIT** See Below Post for more complete solution
Last edited on
Here is a fully functioning version of the program - This is to be used as a reference and learning resource, and not as a copy + paste (how will you ever learn that way?)

There are four "magic numbers", "armstrong numbers", or "narcissistic numbers" between 100 and 1000 and those four "magic numbers" are:
153, 370, 371, and 407 (sum of all cubes equal number)

Run the program and enter those numbers and it will tell you Magic Number, anything else and no cigar.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445 // SumOfCubes.cpp; bitzoID // #include #include using namespace std; int singleDigit(int num, int digit) // Function to seperate each of three digits from userNum { for(int i = 0; i < digit; ++i) num /= 10; // DESTROY PREVIOUS DIGITS return num % 10; // remainder of number div 10 } int calcMagic(int x, int y, int z) { int xT = x * x * x; // variable cubed calculation int yT = y * y * y; int zT = z * z * z; // // int sum = xT + yT + zT; // Cube sums return sum; } int main () { int userNum; cout << "Determine Sum of Cubes is magic number" << endl; while (1) { // repeat program until ctrl+c cout << endl; // Input Single Number to test for narsisistic number cout<<"Input the three digit number: "; cin>>userNum; int x = singleDigit(userNum, 2); // x is return of digit extraction function in 3rd decimal place int y = singleDigit(userNum, 1); // y is return of digit extraction function in 2nd decimal place int z = singleDigit(userNum, 0); // z is return of digit extraction function in 1st decimal place int sum = calcMagic(x,y,z); if (sum == userNum) cout << "You have magic number!" << endl; else cout << "This is NOT a magic number!" << endl; } return 0; }
Last edited on
much appreciated thank you so much! however I this is too advanced for me to understand due to the fact that I just started C++. I will ask my teacher next week for further help! Thanks again!
Topic archived. No new replies allowed.