# C++ help please it due tomorrow and i need help in these two questions left!

• Feb 10th 2013, 01:26 PM
Riana
C++ help please it due tomorrow and i need help in these two questions left!
Example 1
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.

Example 2 (challenging)
Write a program which finds the three digit magic number with the rule from above.
• Feb 10th 2013, 02:27 PM
emakarov
Re: C++ help please it due tomorrow and i need help in these two questions left!
The first digit of a three-digit number n of type int is (n / 100), the second digit is ((n % 100) / 10) and the third one is (n % 10).

To find the magic number, it is possible to search through all three-digit numbers. On modern computers, this is instantaneous.
• Feb 10th 2013, 02:49 PM
Riana
Re: C++ help please it due tomorrow and i need help in these two questions left!
how will i put it in code?
am new so i don't understand it that easy....plz can you show me the code how it will be?
• Feb 10th 2013, 03:05 PM
emakarov
Re: C++ help please it due tomorrow and i need help in these two questions left!
I won't show the full code, particularly since this is an assignment. But I don't understand your difficulty. Here is an outline.

0. It is recommended to declare a function that returns the cube of its argument.

int cube(int n) { return n * n * n; }

1. Declare a variable n of type int.
2. Read a value from the standard input (cin in C++) to n.
3. Write an if-statement. I showed how to get individual digits of n. If n equals cube(first digit) + cube(second digit) + cube(third digit), then output a corresponding string to the standard output (cout).

You should look at examples in your sources to see how to write input/output, arithmetic and if statements.
• Feb 10th 2013, 04:00 PM
Riana
Re: C++ help please it due tomorrow and i need help in these two questions left!
no its not an assignment! its practice questions.
• Feb 12th 2013, 07:52 PM
johng
Re: C++ help please it due tomorrow and i need help in these two questions left!
Hi,
Since this was just practice, I've attached a complete answer to the 2nd question. (You may not have covered functions yet, but surely you must know about loops. Otherwise, I don't see a simple way to find the 3 digit magic numbers.) Since this is a math forum, I pose the following questions:

1. The program found 153, 370, 371 and 407. Prove (maybe with the help of a program) that these are the only integers of any number of digits (>1) with the property that the sum of the cube of the digits is equal to the integer. "Easy."

2. Extend the idea of magic to integers with k digits by saying it is magic iff the sum of the kth powers of the digits is the integer. Prove that for any k, there is a k digit magic number. I think this is hard; I don't have a clue.

The 6 digit integer 548834 has the sum of the 6th powers of the digits equal to itself.

Here's the program:

#include <iostream>

int isMagic(int,int);
int integerPow(int,int);

int main(void) {
int first,last,power=6;
first=integerPow(10,power-1);
last=10*first;
int i;
for (i=first;i<last;i++) {
if (isMagic(i,power)) {
cout<<i<<"is magic for powers"<<power<<endl;
}
}
return(0);
}

// finds and returns b to the k power, could use standard pow, but this is a double
int integerPow(int b,int k) {
int value=1,i;
for (i=1;i<=k;i++) {
value*=b;
}
return(value);
}

// returns true (1) iff sum of the kth power of digits of n is same as n
int isMagic(int n,int k) {
int m=n, digitPowerSum=0,digit;
while (m!=0) {
digit=m%10;
m=(m-digit)/10;
digitPowerSum+=integerPow(digit,k);
}
return(n==digitPowerSum);
}