Java program to find the sum of the digits of a number


Program

package com.candidjava;

import java.util.Scanner;

public class DigitsSum
{
	public static void main(String args[])
	{
		int num, rem = 0, sum = 0, temp;
		Scanner scan = new Scanner(System.in);

		System.out.print("Enter a Number : ");
		num = scan.nextInt();
		scan.close();
		temp = num;

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

		System.out.print("Sum of Digits of " + temp + " is " + sum);
	}
}


Output

Enter a Number : 12345

Sum of Digits of 12345 is 15



Related Post

Comments


©candidjava.com