Results 1 to 9 of 9
Thread: Digits of an integer.
 12302009, 09:31 AM #1Member
 Join Date
 Dec 2009
 Posts
 20
 Rep Power
 0
Digits of an integer.
i have a problem given in the book but i dont know how to deal with it,
problem:
Write a program that displays all integers between low and high that are the SUM OF THE CUBE OF THEIR DIGITS, In other words, find all the numbers xyz
such as xyz = x(cube) + y (cube) + z (cube) , for example
153 = 1(cube) + 5 (cube) + 3(cube). Try 100 for low and 1000 for high.
sorry for using the word cube i dont know how to type the exponent "3" :p
anyway, i totally understand the case but i dont know how to get EACH digit of a given integer, something like the method .charAt(<location>) of the String class, thats where im stuck up. taking EACH digit onebyone.
anyhelp would be appreciated
by the way, the next exercise after this is ,
write a method that returns the number of digits in an integer argumet; for example 23,498 has five digits.
Thats why i need some help about this one.Last edited by Allgorythm; 12302009 at 10:00 AM.
 12302009, 09:51 AM #2Member
 Join Date
 Dec 2009
 Posts
 20
 Rep Power
 0
this is what i've got sor far
pJava Code:ublic class Sample { public static void main(String[] args) { int integer = 153, sum = 0; String convert = "" + integer; char temp = 0; for (int q = 0; q <= convert.length()  1; q++) { temp = convert.charAt(q); sum = sum + (int) temp; } System.out.println(sum); } }
:confused:
 12302009, 12:18 PM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 20
Get the input as a string, and each characters convert into an integer.
 12302009, 12:31 PM #4Member
 Join Date
 Dec 2009
 Posts
 20
 Rep Power
 0
Java Code:public static void main(String[] args) { int integer = 153, sum = 0; String convert = "" + integer; // i even tried this one String convert = "197"; but the output is 167 int temp; for (int q = 0; q <= convert.length()  1; q++) { temp = (int) convert.charAt(q); sum = sum + (int) temp; } System.out.println(sum); }
Last edited by Allgorythm; 12302009 at 12:33 PM.
 12302009, 12:36 PM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 20
Look at the following code.
Java Code:String str= "123"; int iSum = 0; for(int index = 0; index < str.length(); index++) { // Get the int value int iTemp = Integer.parseInt(Character.toString(str.charAt(index))); // Cube iSum += iTemp * iTemp * iTemp; } System.out.println(iSum);
 12302009, 12:47 PM #6Member
 Join Date
 Dec 2009
 Posts
 20
 Rep Power
 0
tnx for that!, that solved it, i forgot the method of Integer class...
 12302009, 12:49 PM #7
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 20
There are lots of ways to do this.
 12302009, 12:50 PM #8Member
 Join Date
 Dec 2009
 Posts
 20
 Rep Power
 0
ahh so this is not the only way to do this program.....
can i ask for a little sample? if it wont bother you...
 01012010, 03:34 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 20
Just think of use an int array to store your input.
Similar Threads

Counting digits in an integer value, including zero
By lithium002 in forum New To JavaReplies: 1Last Post: 12042009, 05:56 AM 
can anyone tell my why this program bugs out when i enter more then 10 digits?
By sweetjava in forum New To JavaReplies: 3Last Post: 08192009, 03:54 AM 
split() by digits
By RobertF in forum New To JavaReplies: 2Last Post: 03122009, 03:16 AM 
sum of digits depreciation
By jleas in forum New To JavaReplies: 13Last Post: 11092008, 02:37 PM
Bookmarks