Program to find sum of digits of a number in java

Let’s see a program to find the sum of digits of a number in java.

package programnew;

import java.util.Scanner;

public class SumofDigits {
	public static void main(String[] args) {
		int n = 0, sum = 0;
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a number");
		n = in.nextInt();

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

		System.out.println("sum of digits is " + sum);

	}
}

Output is :-

Enter a number
78
sum of digits is 15