Here's a Java program to find the sum of all digits of a number:

public class SumOfDigits {

public static int sumOfDigits(int number) {

int sum = 0;

// Iterate through each digit of the number

while (number != 0) {

// Extract the last digit

int digit = number % 10;

// Add the digit to the sum

sum += digit;

// Remove the last digit

number /= 10;


return sum;


public static void main(String[] args) {

int number = 123456;

int sum = sumOfDigits(number);

System.out.println("The sum of digits of " + number + " is: " + sum);




This program defines a method `sumOfDigits` that takes an integer number as input and returns the sum of its digits. It iterates through each digit of the number by continuously extracting the last digit using the modulus operator `%`, adding it to the sum, and then removing the last digit by dividing the number by 10. The process continues until the number becomes 0. Finally, the program prints the sum of digits.