Java Program to Add Digits of a Number

Java Program to Add Digits of a Number

Java Program to Add Digits of a Number

import java.util.Scanner;

class AddDigitsOfANumberClass{
	public static void main(String[] args){
		/* sum variable store sum of digits */

		/* last_digit variable contain last digit 
		of a number until loop termination */
		int sum = 0,last_digit;


		Scanner sc = new Scanner(System.in);

		/* Display message for number input */
		System.out.println("Enter any number");

		/* Number input from user */
		int number = sc.nextInt();

		/* loop continue number remain 0 */
		while(number>0){

			/* get last digit of a number */
			last_digit = number%10;

			/* add last digit to the sum */
			sum = sum+last_digit;

			/* truncate last digit from number */
			number = number/10;
		}

		/* After loop completion results will display */
		System.out.println("Sum of digits are : " + sum);
	}
}
Enter any number
12345
Sum of digits are : 15

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment