codescracker


java

Java Program to Add Digits of Number



« Previous Program Next Program »


Add Digits of Number

To add digits of any number in Java Programming, you have to ask to the user to enter the number to add their digits and display the summation of digits of that number.

Java Programming Code to Add Digits of Number

Following Java program ask to the user to enter the number to add their digits and will display the addition result :

/* Java Program Example - Add Digits of Number */
		
import java.util.Scanner;

public class JavaProgram
{
    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();
		
        temp = num;
		
        while(num>0)
        {
            rem = num%10;
            sum = sum+rem;
            num = num/10;
        }
		
        System.out.print("Sum of Digits of " +temp+ " is " +sum);        
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program add digits of number

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test