# C++ Home Work!

Printable View

• Feb 8th 2013, 06:17 PM
sakonpure6
C++ Home Work!
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!

Code:

```#include <iostream> #include <cmath> 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: ";```
• Feb 8th 2013, 07:03 PM
johng
Re: C++ Home Work!
Add the following to your code:
// you add the code to read digit z
int n = 100*x + 10*y + z, x3 = x*x*x, y3 = y*y*y, z3 = z*z*z;
if (n == x3+y3+z3) {
cout<<"yep";
}
else {
cout<<"nope";
}
return(0);
}

Probably better to let the user enter an integer n and let the program figure out the digits:

int m;
cout<<"enter a three digit positive integer: ";
cin>>m;
if (m <= 0 || m/1000 != 0) {
cout<<"POSITIVE 3 digit integer";
return(1); // just abort
}
int x=m/100, y=(m-100*x)/10, z= m%10;
// now proceed as above, but you don't need n
// just write if (m==x3+y3+z3) ...

P.S. This editor doesn't allow indentation, but I definitely recommend using indentation in your coding.
• Feb 8th 2013, 07:22 PM
jakncoke
Re: C++ Home Work!
#include <iostream>
#include<math.h>
using namespace std;
int main () {
int x;
cout<<"Input the 3 digit number: " ;
cin>>x;
double hundredD = x/100;
double tensD = (x % 100)/10;
double onesD = ((x % 100)%10);
hundredD = pow(hundredD, 3);
tensD = pow(tensD, 3);
onesD = pow(onesD, 3);

int sum = (int)(hundredD + tensD + onesD);
cout<<sum;
if(sum == x)
{
cout<<"Magic Number";
}
else
{
cout<<"Not a Magic Number";
}
return -1;
}
• Feb 8th 2013, 09:07 PM
sakonpure6
Re: C++ Home Work!
can you please explain this part of the code because i don't get it
Quote:

if (m <= 0 || m/1000 != 0)
and
Quote:

int x=m/100, y=(m-100*x)/10, z= m%10
• Feb 8th 2013, 09:28 PM
jakncoke
Re: C++ Home Work!
Quote:

Originally Posted by sakonpure6
can you please explain this part of the code because i don't get it and

The first part)

Basically it exits the program is the user entered a value less than or equal to 0 or they entered more than 3 digits.

The second part)

He does m/100 to get the hundreds digit place. 305/100 = 3.05 (which the computer rounds down to 3).

The second thing he does is multiply the hundreds digit value he got with 100 and minus it by M to get the digits in the tenth and one digits.
For example

if the user put x = 305
then x/100 = 3 (Because computer rounds down) = Hundredth digit
then 305 - 3*100 = 05
then 5/10 = .5 (round down) to 0 (this represents the tenth digit) = Tenths digit
and finally
x % 10 gives you the ones digit = One's digit
so 305 % 10 = 5

so you get 305 back.