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

free web hosting
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.

 

 

 


Reply

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.

Reply

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) :]

Reply

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);
}

 

 

 


Reply

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);
}

Reply

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

Reply


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*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. sum of cube c# program - 423.57 hr back. (1)
Similar Topics

Keywords : sum, cubes, digits, negative, integer

  1. Negative Credits
    Do I have to bring it to 0 first? (9)
  2. Hosting Account Terminated - Still Negative Credits?
    (2)
    Why do I have negative credits when i have already terminated my hosting account? I terminated it a
    few days ago because I wanted to be still semi-active at the forums, but not hosting at astahost.
    After a few days, the forums still showed at I was hosted. I tried terminating again but it failed.....
  3. Checking To See If Something Is Not An Integer
    (4)
    I put together this: CODE //Make Sure $qty is NOT blank before checking if it is a number
        if (!empty($item_1_qty)) { //not empty             }     if
    (intval($item_1_qty) !== $item_1_qty){             //item 1 qty is not
    a whole number             $redirect_to = 'order_form.php?error=6';    
                header("Location: $redirect_to");             exit();
            } But I get my error message coming no matter whether it is an integer or not.....
  4. Data Convertion
    the result of converting to integer is undefined? (0)
    If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), the result of
    converting to integer is undefined? CODE unction StrToNum($Str, $Check,
    $Magic) {     $Int32Unit = 4294967296;  // 2^32     $length =
    strlen($Str);     for ($i = 0; $i < $length; $i++) {
            $Check *= $Magic;              if (is_float($Check)) {
                $Check = ($Check - $Int32Unit * (int) ($Check /
    $Int32Unit)....
  5. Decrementing Number Of Digits Ofter Point
    In C# (15)
    Hi! I've been solving a problem on C# and a one problem had gone off. I need to decrease
    number of digits of a float variable. for example: If I have 14.3413543485 I wanna make it just
    14.34. My Informatic teacher says there is a way, but he'd forgot it. Could you, please, help
    me?....
  6. A Small Project: Splitting An Integer Into Digits
    (5)
    I need to know how to "split" a number up into seperae numbers, using VB. For example, "54321". I
    need to split that into 5 seperate numbers - "5," "4," "3," "2," "1," and assign them to a variable,
    if possible. This next part isn't important, but if you're curious why I need to know ...
    Eventually what I need to do is split the number into these individual parts and add them together.
    With that new number I need to again split it and take the last number subtracted from 10. That new
    number will then be placed at the end of the beginning number. Example: 5+4+3....
  7. Members With Thousands Of Negative Credits: Read
    (10)
    Hi folks, I believe something went wrong with the server time last night. People who've posted
    during this period might be affected with the credit problem - somehow the server time got advanced
    to June 2018 - as demostrated by this post:
    http://www.astahost.com/index.php?showtopi...indpost&p=61902 Naturally, when the new credits were
    calculated following a post, the time difference completely messed it up - as it calculated the
    difference of yesterday to 13 years later and alloted that many negative credits... lol.. sorry - I
    can't help but laugh at this st....
  8. Hosting Credits : (-)4599.74 (negative)
    huh? (6)
    Uhm, is this just a bug? I sure hope so because I'm pretty sure I wasnt inactive for 4000
    days! /ohmy.gif' border='0' style='vertical-align:middle' alt='ohmy.gif' /> Err, also, is
    there a place to report bugs? I didnt notice one.....
  9. Negative Credits? Suspended? What's Going On?
    I can't login to my hosted account... (4)
    I just signed up for a free hosted account a week or so ago and left for some much needed vacation
    time. I just got back today and came on here to check the posts and what-not and to get my site
    started when I see this at the top: QUOTE HOSTING CREDITS : (-)7.42 (Negative) Your Hosting
    Credits have Expired. You will need atleast 4 credits to UN-SUSPENDED your hosting Account. Account
    is UN-suspended only when a user has more than 3 hosting credits. You get credits by making forum
    posts. After you make the required posts, your hosting account will be active automati....

    1. Looking for sum, cubes, digits, negative, integer

*RANDOM STUFF*





*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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE