Hello All,

Here are the instructions for the program I am trying to write:

Write a program that determines which 2, 3, and 4 digit #'s are equal to the sum of the cubes of their digits.
Ex.
if the original # is 234, determine if (2 cubed) + (3 cubed) + (4 cubed) is equal to 234.

The range of #'s is from 10 to 9999. I have gotten a result for the 3 digit #'s but not the 2 or 4. Please let me know if I am missing something or anyone has any suggestions

Thanks

int num, num1, num2, num3, dig1, dig2, dig3, dig4, sum;
 cout <<"Section two results:" << endl;
 print <<"Section two results:" << endl;
 while (num <= 9999)
  {
       dig4 = num % 10;
       num2 = num / 10;
       dig3 = num2 % 10;
       num3 = num2 / 10;
       dig2 = num3 % 10;
       dig1 = num3 / 10;
       sum = (dig1 *dig1 * dig1) + (dig2 * dig2 * dig2) + (dig3 * dig3 * dig3) 
       << + (dig4 * dig4 * dig4);
       if (sum == num)
       {
          cout << " " << sum << endl;
          print << " " << sum << endl;
       }//end if
       num++;
  }//end while  


  num = 100;
  while (num <= 999)
  {
       dig3 = num % 10;
       num2 = num / 10;

       dig2 = num2 % 10;
       dig1 = num2 / 10;
       sum = (dig1 * dig1 * dig1) + (dig2 * dig2 * dig2) + (dig3 * dig3 * dig3);
       if (sum == num)
       {
          cout << " " << sum << endl;
          print << " " << sum << endl;
       }//end if
       num++;
  }//end while  

  cout << endl << endl;
  num = 10;
  while (num <= 99)
  {


       dig2 = num % 10;
       dig1 = num / 10;
       sum = (dig1 * dig1 * dig1) + (dig2 * dig2 * dig2);
       if (sum == num)
       {
          cout << " " << sum << endl;
          print << " " << sum << endl;
       }//end if
       num++;
  }//end while 



 frz;
 return 0;

Recommended Answers

All 7 Replies

Salem,

Is this something I need to download and install or is this just a code that i type in?

I appreciate all the help.

Jordan

there are two things you could do to help those who want to help you...

first of all, wrap your text in code tags[[B]code=c[/B]][[B]/code[/B]]...

second, try using a title that suggests what your problem is next time...

man... you have serious problems with basic c++ programming...

first, you have no need of using that much variables... i guess three variables will be enough...

second... why all those validations? i really don't understand what is the idea before every while loop...

perhaps you should consider wrapping your text in code, that way, we could read it better...

man... you have serious problems with basic c++ programming...

DUDE...

take it easy... lol

i am sure you were one day a beginner too...

PS:
i am one now

dude....

they are right tho..

it does help readers if code is slightly more organised

ok... maybe that was a little rough :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.