Sum of Digits in Java

Write a Program to Find Sum of Digits in Java using For Loop, While Loop, Functions, and Recursion

Sum of Digits in Java using While Loop

This program allows the user to enter any positive integer. Then it will divide the given number into individuals and add those individuals (Sum) digits using Java While Loop.

package SimplerPrograms;

import java.util.Scanner;

public class SumofDigitsUsingWhile {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number, Reminder, Sum = 0;
		sc = new Scanner(System.in);		
		System.out.println("Please Enter any Number: ");
		Number = sc.nextInt();
		
		while(Number > 0) {
			Reminder = Number % 10;
			Sum = Sum+ Reminder;
			Number = Number / 10;
		}
		System.out.format("Sum of the digits of Given Number = %d", Sum);
	}
}
Program to Find Sum of Digits in Java 1

In this program, first, We declared three integer variables Number, Remainder, and Sum = 0. The first two statements will ask the user to enter any positive integer. Then, that number assigned to variable Number.

Next, the Condition inside the While Loop will make sure that the given number is greater than 0 (Means Positive integer and greater than 0)

Let us see the working principle of this while loop in iteration wise.

From the above Java sum of digits of a number screenshot, the user entered value: Number= 9876.

First Iteration

For the first Iteration, Number = 9876 and Sum = 0. It means, While (9876 > 0) is TRUE. So, the program will start executing statements inside the while loop

Reminder = Number% 10
Reminder = 9876 % 10 = 6

Next, we have to find the Sum
Sum= Sum+ Reminder
Sum= 0 + 6 = 6

Lastly, we have to remove the last digit from the user-specified number
Number = Number/ 10
Number= 9876 / 10 = 987

Second Iteration of the Sum of Digits in Java program

From the first Iteration, the values of both Number and Sum changed as Number = 987 and Sum = 6. It means, While (987 > 0) is TRUE. So, the program will start executing statements inside the while loop.

Reminder = 987 % 10 = 7

Sum= 6 + 7 = 13

Number= 987 / 10 = 98

Third Iteration

From the Second Iteration of Java sum of digits of a number program, the values of  Number= 98 and Sum= 13. It means, While (98 > 0) is TRUE

Reminder = 98 % 10 = 8

Sum= 13 + 8 = 21

Number= 98 / 10 = 9

Fourth Iteration

From the third Iteration, Number = 9 and Sum = 21. It means, While (9 > 0) is TRUE.

Reminder = 9 % 10 = 9

Sum= 21 + 9 = 30

Number= 9 / 10 = 0

Here Number= 0. So, the while loop condition will fail.

The last System.out.format statement will print the Sum variable as the output. So, the output of the given variable 9876 = 30.

Program to Find Sum of Digits in Java using For Loop

This program allows the user to enter any positive integer. Then it will divide the given number into individual digits and find the sum of those individuals using Java For Loop.

package SimplerPrograms;

import java.util.Scanner;

public class UsingFor {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number, Reminder, total = 0;
		sc = new Scanner(System.in);		
		System.out.println("Please Enter any Number: ");
		Number = sc.nextInt();
		
		for (total = 0; Number > 0; Number = Number/10){
			Reminder = Number % 10;
			total = total + Reminder;
		}
		System.out.format("Sum of the digits of Given Number = %d", total);
	}
}

We just replaced the While loop in the above example with the For loop. Please refer to Java For Loop.

Please Enter any Number: 
2345
Sum of the digits of Given Number = 14

Program to Find Sum of Digits in Java using Functions

This sum of digits in the Java program allows the user to enter any positive integer value. Next, we are going to pass the User entered value to the Method we created.

Within this User defined function, this sum of digits program will divide the given number into individual items and adding those individuals using the For Loop

package SimplerPrograms;

import java.util.Scanner;

public class UsingMethods {
	private static Scanner sc;
	private static int total = 0;
	public static void main(String[] args) {
		int Num;
		sc = new Scanner(System.in);		
		System.out.println("Please Enter any Value: ");
		Num = sc.nextInt();
		
		total = SOD(Num);
		System.out.format("Sum of the digits = %d", total);
	}
	public static int SOD(int Num) {
		int Reminder;
		for (total = 0; Num > 0;Num = Num/10) {
			Reminder = Num % 10;
			total = total+ Reminder;
		}
		return total;
	}
}
Please Enter any Value: 
3698
Sum of the digits = 26

In this program, we are calling the SOD(Number) method. As we all know that the method will return integer value so, we are assigning that return value to Sum

total = SOD(Num);

When the compiler reaches to SOD(Num) line, the compiler will immediately jump to below function:

public static int SOD(int Num) {

We already explained the LOGIC in the above example.

Program to Find Sum of Digits in Java using Recursion

This example allows the user to enter any positive integer. Then the java program will divide the given number into individual digits and adding those individuals as Sum by calling the function recursively.

In this program, we are dividing the code using the Object-Oriented Programming. To do this, First, we will create a class that holds a method to reverse an integer recursively.

package SimplerPrograms;

public class SoDigi {
	int Sum = 0, Reminder;
	public int SDCalc(int Number) {
		if(Number > 0) {
			Reminder = Number % 10;
			Sum = Sum + Reminder;
			SDCalc(Number / 10);		
			return Sum;
		}
		else {
			return 0;
		}
	}
}

Within the Main program, we will create an instance of the above-specified class and call the methods

package SimplerPrograms;

import java.util.Scanner;

public class UsingRecursion {
	private static Scanner sc;

	public static void main(String[] args) {
		int Number, Sum = 0;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter any : ");
		Number = sc.nextInt();
		
		SoDigi sd = new SoDigi();
		Sum = sd.SDCalc(Number);
		
		System.out.format("\n Sum of the digits = %d", Sum);
	}
}
Please Enter any : 
456789
Sum of the digits = 39

SoDigi Class Analysis:

Within this Java sum of digits program example, we defined a function in this class. The following function will accept integer value as a parameter value and return an integer value.

public int SDCalc(int Number) {

Let us see the If Else statement inside the above-specified function. If (Number > 0) will check whether the given number is greater than zero or not.

  • If the condition is True, then the Java compiler will return the Sum of digits in a given number. Here, we are using SDCalc(Number / 10); statement inside the If block because it will help to call the function Recursively with the updated value. When you miss this statement, then after completing the first line, it will terminate. For example, Number = 4567 will return the output as 7
  • If the condition is False, then the compiler will return the Sum as Zero.

Main Class Analysis:

In the main class of the program, we created an instance / created an Object of the SoDigi Class

SoDigi sd = new SoDigi();

Next, we are calling the SDCalc(Number) method. We all know that the method will return an integer value. So, we are assigning that return value to Sum.

Sum = sd.SDCalc(Number);

Comments are closed.