Digit Sum with while

Lab Java Beginner 10 points

Read a non-negative integer and print the sum of its digits using a while loop.

Input

One integer.

Output

The digit sum.

Example

Input

12345

Output

15
Sample input available Sample output available
Editor
Java

Hints

n % 10 is the last digit; n /= 10 removes it.

Solution explanation

while is the natural loop when you don't know how many steps you need.

Show solution
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        System.out.println(sum);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD