Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By JosAH

Thread: Summing the digit

  1. #1
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default 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;
    	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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    Thanks for the reply but unable to understand your code.

  4. #4
    amanrv is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    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. #5
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    awesome man, really impressed.

  6. #6
    amanrv is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    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. #7
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    but how can i become like you.

  8. #8
    amanrv is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    the feeling will only come if you keep practicing, and of course, if you actually are serious about programming

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gozuhair View Post
    Thanks for the reply but unable to understand your code.
    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
    Junky likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by amanrv View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    gozuhair is offline Member
    Join Date
    Jul 2011
    Posts
    39
    Rep Power
    0

    Default

    Thank you very much, will bother you again.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

    Default

    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. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2011, 12:37 AM
  2. 4 digit restriction
    By GTM in forum Java Applets
    Replies: 3
    Last Post: 12-28-2010, 02:26 AM
  3. summing value within Hashtable
    By dc0m in forum New To Java
    Replies: 4
    Last Post: 09-23-2010, 12:17 AM
  4. Help with summing series
    By xplsivo in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 07:37 PM
  5. digit spacing
    By puk284 in forum New To Java
    Replies: 3
    Last Post: 06-09-2009, 02:49 PM

Posting Permissions

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