Jump to content
xisto Community
demolaynyc

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

Recommended Posts

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

:P 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-----

/* 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.

Share this post


Link to post
Share on other sites

:P 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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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:

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

Edited by demolaynyc (see edit history)

Share this post


Link to post
Share on other sites

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... :P 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.

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

Share this post


Link to post
Share on other sites

I believe this code is exactly what you need.

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 :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.