Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Write a Java program and compute the sum of the digits of an integer.

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a Java program and compute the sum of the digits of an integer.

    I need help with a program where I have to find the sum of the digits of an integer.

    For example, for the integer 32, the sum of the digits of the integer is: 3+2 = 5
    Or, for the integer 321, the sum of the digits of the integer is: 3+2+1 = 6

    One person gave me this code (but did not explain):
    class Digits
    {
    	public static void main(String args[])
    	{
    		int num = 25;
    		int sum = 0; 
     
    		while (num > 0) 
    		{
    			sum = (sum + num) % 10;
    			num = num / 10; 
    		}
    		System.out.println("The sum of the digits of 25 is: " + sum);
    	}
    }

    However, I'm having trouble understanding what's going on in the while statement. BTW, i've even tried it with the numbers above (32 and 321), but I still don't understand the input?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write a Java program and compute the sum of the digits of an integer.

    what's going on in the while statement.
    It uses the value in num to control looping. As long as num > 0, it continues to loop.
    To see how the value of num changes add a print statement to print the value of num at the end of the loop just before the ending }
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sum of Digits in Java
    By edward07 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 4th, 2014, 02:44 AM
  2. Replies: 2
    Last Post: October 25th, 2013, 06:31 AM
  3. sum of digits
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 02:40 PM
  4. Rookie here, need help with sum of digits program
    By wkellogg10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2013, 07:25 PM
  5. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM