Nov 8, 2009

The Sum Of The Cubes Of The Digits Of Any Non-negative Integer

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

The Sum Of The Cubes Of The Digits Of Any Non-negative Integer

demolaynyc
Ok I need someone to help me tackle this program. Here's the question:

Exercise 9

a) Write a program that displays the sum of the cubes of the digits of any non-negative integer. Two program runs are shown below:

Enter an integer: 145
Sum of digits is 10

Enter an integer: 8888
Sum of the cubes of the digits is 2048

cool.gif Modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digits.



----source file for part A-----
CODE
/* Chapter 4 Exercise 9 by Albert Villaroman 11-20-06 */

#include <iostream.h>

int main() {

    long numValue; //user input
    cout <<"Enter an integer: ";
      cin >>numValue;

    long MaxDigits = 100000000;
    
    while (MaxDigits > numValue) { //while loop to get how many digits numValue has
        MaxDigits /= 10;
    }

    long sum = 0;    //sum of all digits
    
    
    while (MaxDigits >=1) {
            long digit = numValue / MaxDigits; //get digit
            sum += digit*digit*digit;
            numValue = numValue % MaxDigits; //enter new value for numValue
            MaxDigits /= 10;       //ex: 100 => 10
        }
    
    
        int counter = 10;
    while (counter < 90000) {
            if (sum == numValue) {
                cout <<numValue;
            }
            counter++;
        }

    

    cout <<"Sum of the cubes of the digits is " <<sum <<endl;

    

    return(0);
}


What I need help with is part B.

Can someone paraphrase the part B question for me because it just doesn't make sense to me. I'd appreciate a full source file of the second part if you can.

Thank you.

 

 

 


Comment/Reply (w/o sign-up)

pyost
QUOTE(demolaynyc @ Dec 4 2006, 07:35 PM) *

cool.gif Modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digits.


First of all, you need to go through all numbers from 10 to 9999. Then, by using this code, you will calculate the sum of the cubes of the digits for every number. After that, you will check whether the number itself is equal to the previously calculated sum. If it is, that is one of the numbers you are looking for.

Comment/Reply (w/o sign-up)

Arbitrary
QUOTE

cool.gif Modify the program to determine what integers of two, three, or four digits are equal to the sum of the cubes of their digits.

Hmm...following whatever Pyost said should get you somewhere, but I just wanted to add my two cents. Since you're going to be reusing the whole "get-the-cube-of-the-sum-of-the-digits" code, you might as well put that into a method and then call that method every time you want to use the code. It'll probably save a bit of space (as well as some headaches) :]

Comment/Reply (w/o sign-up)

demolaynyc
Yea, I agree but since I'm still learning C++, I wouldn't really know how to do it. Even if, my instructor would get a little suspicious.

Hey pyost, can you put what you just said into the C++ coding because I understand better if i see the code. Thanks

PS: I think this exercise is stupid I mean will it even be useful in real programming? When would be the time to put this into application, does anyone know? I'm not talking about C++ in general, it's just this exercise specifically.




---Hold up---

So is part B a separate program? I should just use the formula from part A? This exercise is really confusing.

---
Alright here's the code that I came up with:

CODE

/* Chapter 4 Exercise 9 by Albert Villaroman 11-20-06 */

#include <iostream.h>

int main() {

    long MaxDigits = 100000000, sum = 0, num = 10;
                              //sum of all digitswhile (num < 9999) {    
  
    
    while (num < 50) {
         int numBeforeCalc = num;
         cout <<"this is num: " <<num <<endl;
         while (MaxDigits > num) { //while loop to get how many digits num has
               MaxDigits /= 10;
               }
    
         while (MaxDigits >= 1) {
               long digit = num / MaxDigits; //get digit
               cout <<"digit: " <<digit <<endl;
               sum += digit*digit*digit;
               num = num % MaxDigits; //enter new value for numValue
               MaxDigits /= 10;       //ex: 100 => 10
               }
    
         cout <<sum <<"  " <<numBeforeCalc <<endl;
         if (sum == numBeforeCalc)
            cout <<"Number that equals original number. " <<numBeforeCalc <<endl;
        
         num = numBeforeCalc;
         num += 1;
    } //end big while
    
    int q;
      cin >>q;
    

    return(0);
}

 

 

 


Comment/Reply (w/o sign-up)

yeh
Yeah... I can understand your predicament. Sometimes the question posed to you by your teacher/lecturer is just plain vague. Heck, sometimes what they want and what the question want is entirely different. So, I suggest you go ask your teacher what it is that he/she wants. Ask him/her for a sample of input and output so you can better know the question.

Now, what pyost and arbitrary said is actually correct. But since you asked for some code, here is some help. Hehe.... I'm going to embarass myself here if it turns out to be wrong... smile.gif Anyway, feel free to correct me if you think what I'm suggesting is wrong. The code below loops through 10 to 9999, computing their sum of the cubes of their digits and then compare the sum to the original number to see whether they are the same. I try to use as much code from your original post as possible.

CODE
#include <iostream.h>

int main() {

    counter=10;

    while(counter<=9999)
    {
     numvalue = counter;

    //From original code    
     long MaxDigits = 100000000;
    
    while (MaxDigits > numValue) { //while loop to get how many digits numValue has
        MaxDigits /= 10;
    }

    long sum = 0;    //sum of all digits
    
    
    while (MaxDigits >=1) {
            long digit = numValue / MaxDigits; //get digit
            sum += digit*digit*digit;
            numValue = numValue % MaxDigits; //enter new value for numValue
            MaxDigits /= 10;       //ex: 100 => 10
        }
    //End of original code

      if(counter==sum)
    cout<<counter<<endl;
    

     counter++;
    }

    return(0);
}

Comment/Reply (w/o sign-up)

pyost
I believe this code is exactly what you need.

QUOTE(demolaynyc @ Dec 5 2006, 02:42 AM) *

I think this exercise is stupid I mean will it even be useful in real programming? When would be the time to put this into application, does anyone know? I'm not talking about C++ in general, it's just this exercise specifically.


I shared the same opinion a year ago, when I started programming, but it turned out to be quite different. Sure, some examples aren't really useful when it comes to real programming, but they show you how to deal with some basics (like this one). And it will all be necessary at some point. I am currently developing a PHP application, and whereas I have learned Pascal in school, not PHP, it has helped me a lot. Not because of the code itself, but because of the problem-solving techniques.

So just don't complain, it's worth it biggrin.gif

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Sum Cubes Digits Negative Integer


    Looking for sum, cubes, digits, negative, integer

See Also,

*SIMILAR VIDEOS*
Searching Video's for sum, cubes, digits, negative, integer
advertisement



The Sum Of The Cubes Of The Digits Of Any Non-negative Integer

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com