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!
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <cmath>
usingnamespace 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: ";
// MagicNumbers.cpp - zoID
//
#include <iostream>
#include <cmath>
usingnamespace 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.
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.
// SumOfCubes.cpp; bitzoID
//
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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!