Results 1 to 5 of 5
Like Tree2Thanks
  • 1 Post By johng
  • 1 Post By jakncoke

Math Help - C++ Home Work!

  1. #1
    Senior Member sakonpure6's Avatar
    Joined
    Sep 2012
    From
    Canada
    Posts
    378
    Thanks
    27

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

  2. #2
    Super Member
    Joined
    Dec 2012
    From
    Athens, OH, USA
    Posts
    611
    Thanks
    248

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

  3. #3
    Senior Member jakncoke's Avatar
    Joined
    May 2010
    Posts
    388
    Thanks
    80

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

  4. #4
    Senior Member sakonpure6's Avatar
    Joined
    Sep 2012
    From
    Canada
    Posts
    378
    Thanks
    27

    Re: C++ Home Work!

    can you please explain this part of the code because i don't get it
    if (m <= 0 || m/1000 != 0)
    and
    int x=m/100, y=(m-100*x)/10, z= m%10
    Last edited by sakonpure6; February 8th 2013 at 08:26 PM.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Senior Member jakncoke's Avatar
    Joined
    May 2010
    Posts
    388
    Thanks
    80

    Re: C++ Home Work!

    Quote Originally Posted by sakonpure6 View Post
    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.
    Last edited by jakncoke; February 8th 2013 at 08:35 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. home work
    Posted in the Algebra Forum
    Replies: 2
    Last Post: July 30th 2012, 05:58 AM
  2. i need help with my 7th grade home work.
    Posted in the Algebra Forum
    Replies: 1
    Last Post: November 30th 2009, 04:16 PM
  3. math home work help
    Posted in the Algebra Forum
    Replies: 1
    Last Post: September 11th 2008, 06:07 AM
  4. [SOLVED] Stuck on Maths Home work :(
    Posted in the Pre-Calculus Forum
    Replies: 1
    Last Post: November 13th 2007, 09:11 AM
  5. home work #3 plz help ASAP
    Posted in the Statistics Forum
    Replies: 8
    Last Post: June 24th 2006, 07:46 PM

Search Tags


/mathhelpforum @mathhelpforum