# Summing the digit

• 07-17-2011, 11:49 AM
gozuhair
Summing the digit
Hi All
I am in the process of learning java, could you please help me out, my query is that :
I want to create such type of program that accept integer no between 0 to 1000 and then extract each digit and summarize it.
for instance: if we input 934 integer , the sum of all its digits is 14.(9+3+4=16).
I create the following code.
Code:

```import java.util.Scanner; public class aumminginteger {         public static void main(String[] args)         {         Scanner input        =        new Scanner(System.in);                 int forsumming;         int fortest1;         int fortest2;         int fortest3;         int fortest4;         int forsum=0;         System.out.println("Please Enter your no");         int forinput=input.nextInt();         fortest1=forinput        % 10;         fortest2=forinput        / 10;         fortest3=fortest2        % 10;         fortest4=fortest2        / 10;         forsum=fortest1        +        fortest3        +        fortest4                ;                                         System.out.println("For test 1        "+fortest1);         //                System.out.println("For test 2        "+fortest2);                         System.out.println("For test 3        "+fortest3);                         System.out.println("For test 4        "+fortest4);                                 System.out.println(forsum);                 } }```
The above code is working fine with 3 digits but its not working fine with 4 digits.I also want your advices regarding improvements in the above codes. Thanks
• 07-17-2011, 12:11 PM
JosAH

Code:

```int sumOfDigits(int n) {   if (n < 10) return n;   return n%10+sumOfDigits(n/10); }```
kind regards,

Jos
• 07-17-2011, 12:44 PM
gozuhair
• 07-17-2011, 12:50 PM
amanrv
its simple if you know the while loop. here's your answer. for your understanding, i am using 924 for summing up the digits.

int n=924;
int a=0;
int sum=0;
while(n>0)
{
a=n%10;
sum=sum+a;
n=n/10;
}

this code will work for numbers of any length
• 07-17-2011, 12:54 PM
gozuhair
awesome man, really impressed.
• 07-17-2011, 01:03 PM
amanrv
oh, it was a pleasure. i love programming, you know. if you've seen the movie 'the sister act part 2', there's a spectacular line in it - if you wake up in the morning and the first thing that comes to your mind is writing, then you are a writer.
odd it may sound, when i wake up in the morning, i have the same feeling, except it is for writing code.
• 07-17-2011, 01:05 PM
gozuhair
but how can i become like you.
• 07-17-2011, 01:12 PM
amanrv
the feeling will only come if you keep practicing, and of course, if you actually are serious about programming
• 07-17-2011, 01:12 PM
JosAH
Quote:

Originally Posted by gozuhair

My good old schizophrenia example comes in nicely here: suppose I'm bluffing and telling everybody that I can sum the digits in a number; to tell you the truth I can only do it for numbers less than ten (it must be a single digit number and even I can do that); for all other numbers, I ask my very clever, but very shy, friend to do it; so basically this is me:

Code:

```int me(int n) {   if (n < 10) return n; // I can do the simple stuff   return n%10+myFriend(n/10); // my friend does the hard part and I add the rightmost digit to it. }```
To tell you the truth again: I don't haVe a clever, but shy, friend; I made him up; he's all in my schizophrenic imagination; he is me, so:

Code:

```int me(int n) {   if (n < 10) return n;   return n%10+me(n/10); }```
Got it?

kind regards,

Jos
• 07-17-2011, 01:14 PM
JosAH
Quote:

Originally Posted by amanrv
oh, it was a pleasure. i love programming, you know. if you've seen the movie 'the sister act part 2', there's a spectacular line in it - if you wake up in the morning and the first thing that comes to your mind is writing, then you are a writer.
odd it may sound, when i wake up in the morning, i have the same feeling, except it is for writing code.

When I wake up in the morning, all I think is 'coffee' and 'tobacco' ...

kind regards,

Jos
• 07-17-2011, 01:17 PM
gozuhair
Thank you very much, will bother you again.
• 07-17-2011, 01:32 PM
sunde887
To get better at programming it's invaluable to figure out as much as possible by yourself. The more you struggle when learning, the better. Following a good book with exercises will help very much.
• 07-18-2011, 06:47 AM
gozuhair
i tried to read the book Head first java but its not friendly then i tried to read Introduction to Java 6th edition by Y.Daniel Liang then i found it very freindly so far.
• 07-18-2011, 07:09 AM
Junky
@ gozuhair and amanrv

Geez, get a room. ;)