ADVERTISEMENT
ADVERTISEMENT

Java program to find sum of all digits

In this Java program, we are going to learn how to find sum of all digits? Here, we are reading an integer number and finding sum of its all digits.
Submitted by IncludeHelp, on November 15, 2017

Given a number and we have to find sum of its all digits using java program.

Example 1:

    Input:
    Number: 852
    Output:
    Sum of all digits: 15

Example 2:

    Input:
    Number: 256868
    Output:
    Sum of all digits: 35

Program to find sum of all digits in java

import java.util.Scanner;

public class AddDigits
{
    public static void main(String args[])
    {
    	// initializing and declaring the objects.
        int num, rem=0, sum=0, temp;
        Scanner scan = new Scanner(System.in);
		
        // enter number here.
        System.out.print("Enter the Number : ");
        num = scan.nextInt();
		
        // temp is to store number.
        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

First run:
Enter the Number : 582
Sum of Digits of 582 is : 15

Second run:
Enter the Number : 256868
Sum of Digits of 256868 is : 35

Java Basic Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.