Java Program to Find sum of Digits


Prev Tutorial Next Tutorial

Java Program to Find sum of Digits

To Find sum of digits means add all the digits of any number, for example we take any number like 358. Its sum of all digits is 3+5+8=16. Using given code we can easily design this program.


import java.util.Scanner;
class Sumofdigit
public static void main(String[] args) 
int a,no,sum=0;
Scanner s=new Scanner(;
System.out.println("Enter any number: ");
System.out.println("Sum of Digits :"+sum);


Enter Value in a :
Enter Value in b :
Value in a : 20
Value in b : 10

Syntax to compile and run java program


for compile -> c:/javac
for run -> c:/java Sumofdigit

Explnation of Code

  • Scanner s=new Scanner( are used for receive input from keyboard.
  • nextInt(): method are used for get integer type value from keyboard.
  • System.out.println("....."): are used for display message on screen or console.

Explanation of program


Here we find remainder of given number, using Modulo Operator we find remainder of any number, Using this step we get only last digits.


Here After dividing any number by 10 we get all digits except last digits.


Using this statement we find sum of all digits. In first iteration it add 0 and last digits and store in Sum variable, in second iteration it add Previous sum values and last digits of new number and again store in Sum variable, and so on upto while condition is not false.

Prev Tutorial Next Tutorial


Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements

Buy Websites 9999595223
wifi cctv camera

Wifi CCTV Camera RS:1200 to 2499. Contact no. 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: or 9999595223 Try this Keyword C++ Programs