Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    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.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Joined
    Oct 2009
    Posts
    5,501
    Thanks
    765

    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.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    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?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    MHF Contributor
    Joined
    Oct 2009
    Posts
    5,501
    Thanks
    765

    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.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

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

    no its not an assignment! its practice questions.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Super Member
    Joined
    Dec 2012
    From
    Athens, OH, USA
    Posts
    607
    Thanks
    247

    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);
    }
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Math Final Tomorrow! :S Probability Question.
    Posted in the Statistics Forum
    Replies: 1
    Last Post: June 16th 2009, 07:27 PM
  2. Help!-Don't get this question Due tomorrow!
    Posted in the Math Topics Forum
    Replies: 3
    Last Post: September 22nd 2008, 05:15 PM
  3. Replies: 2
    Last Post: July 15th 2008, 07:14 AM
  4. Replies: 2
    Last Post: May 3rd 2007, 10:30 AM
  5. Exam Tomorrow! Mechanics quick question!
    Posted in the Advanced Applied Math Forum
    Replies: 7
    Last Post: January 9th 2007, 01:14 PM

Search Tags


/mathhelpforum @mathhelpforum