1. Member
Join Date
Jul 2011
Posts
39
Rep Power
0

## 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.
Java 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;
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

2. Recursion is your friend here:

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

Jos

3. Member
Join Date
Jul 2011
Posts
39
Rep Power
0

4. Member
Join Date
Jul 2011
Posts
12
Rep Power
0
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

5. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
awesome man, really impressed.

6. Member
Join Date
Jul 2011
Posts
12
Rep Power
0
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.

7. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
but how can i become like you.

8. Member
Join Date
Jul 2011
Posts
12
Rep Power
0
the feeling will only come if you keep practicing, and of course, if you actually are serious about programming

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

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

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

kind regards,

Jos

10. 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

11. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
Thank you very much, will bother you again.

12. 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.

13. Member
Join Date
Jul 2011
Posts
39
Rep Power
0
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.

14. @ gozuhair and amanrv

Geez, get a room. ;)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•