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

## Program to Find Sum of Digits in Java using While Loop

This sum of digits in Java program allows the user to enter any positive integer and then it will divide the given number into individual digits and adding those individual (Sum) digits using Java While Loop

// Write a Program to Find Sum of Digits in Java using 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); } }

**OUTPUT**

**ANALYSIS**

In this Java sum of digits program, first, We declared three integer variables Number, Remainder and Sum = 0. Following statements will ask the user to enter any positive integer and then, that number is assigned to variable Number.

System.out.println("Please Enter any Number: "); Number = sc.nextInt();

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)

while(Number > 0) { Reminder = Number % 10; Sum = Sum+ Reminder; Number = Number / 10; }

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

From the above Java sum of digits of a number screenshot you can observe that, User entered value: Number = 9876

**First Iteration of the Java Sum of Digits program**

For the first Iteration, Number = 9876 and Sum = 0. It means, * While (9876 > 0)* is TRUE so, 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 user specified number

Number = Number / 10

Number = 9876 / 10 = 987

**Second Iteration**

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

Reminder = Number % 10

Reminder = 987 % 10 = 7

Next, we have to find the Sum

Sum = Sum + Reminder

Sum = 6 + 7 = 13

Lastly, we have to remove the last digit from user specified number

Number = Number / 10

Number = 987 / 10 = 98

**Third Iteration**

From the Second Iteration of Java sum of digits of a number program, the values of both Number and Sum has been changed as: Number = 98 and Sum = 13. It means, * While (98 > 0)* is TRUE so, program will start executing statements inside the while loop

Reminder = Number % 10

Reminder = 98 % 10 = 8

Next, we have to find the Sum

Sum = Sum + Reminder => 13 + 8

Sum = 21

Lastly, we have to remove the last digit from user specified number

Number = Number / 10 => 98 / 10

Number = 9

**Fourth Iteration**

From the Fourth Iteration the values of both Number and Sum has been changed as: Number = 9 and Sum = 21. It means, * While (9 > 0)* is TRUE so, program will start executing statements inside the while loop

Reminder = Number % 10

Reminder = 9 % 10 = 9

Next, we have to find the Sum

Sum = Sum + Reminder

Sum = 21 + 9 = 30

Lastly, we have to remove the last digit from user specified number

Number = Number / 10 => 9 / 10

Number = 0

**Here **Number = 0 so, the while loop condition will fail

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

System.out.format("Sum of the digits of Given Number = %d", Sum);

## Program to Find Sum of Digits in Java using For Loop

This program for sum of digits in java allows the user to enter any positive integer and then it will divide the given number into individual digits and adding those individual (Sum) digits using Java For Loop

**JAVA CODE**

// Write a Program to Find Sum of Digits in Java using For Loop package SimplerPrograms; import java.util.Scanner; public class SumofDigitsUsingFor { 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(); for (Sum = 0; Number > 0; Number = Number/10){ Reminder = Number % 10; Sum = Sum+ Reminder; } System.out.format("Sum of the digits of Given Number = %d", Sum); } }

We just replaced the While loop in the above Java sum of digits example with the For loop. If you find difficult to understand the for loop, please refer For Loop article here: Java For Loop

**OUTPUT**

## Program to Find Sum of Digits in Java using Functions

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

Within this User defined function, this Java sum of digits program will divide the given number into individual digits and adding those individual (Sum) digits using the Java For Loop

**JAVA CODE**

// Write a Program to Find Sum of Digits in Java Using Functions package SimplerPrograms; import java.util.Scanner; public class SumofDigitsUsingMethods { private static Scanner sc; private static int Sum = 0; public static void main(String[] args) { int Number; sc = new Scanner(System.in); System.out.println("Please Enter any Number: "); Number = sc.nextInt(); Sum = SumofDigits(Number); System.out.format("Sum of the digits of Given Number = %d", Sum); } public static int SumofDigits(int Number) { int Reminder; for (Sum = 0; Number > 0;Number = Number/10) { Reminder = Number % 10; Sum = Sum+ Reminder; } return Sum; } }

**OUTPUT**

**ANALYSIS:**

In this Java sum of digits program, we are calling the *SumofDigits(Number)* method. As we all know that the method will return integer value so, we are assigning that return value to Sum

Sum = SumofDigits(Number);

When the compiler reaches to SumOfDigits (Number) line in main() program, the compiler will immediately jump to below function:

public static int SumofDigits(int Number) {

We already explained the LOGIC in above Java sum of digits of a number example.

**NOTE: **If we create a function with Void then there is no need to return any value but, if we declared a function with any data type (int, float etc) then we have return something out from the function.

## Program to Find Sum of Digits in Java using Recursion

This sum of digits in java program allows the user to enter any positive integer and then it will divide the given number into individual digits and adding those individual (Sum) digits by calling the function recursively.

In this sum of digits in Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a method to reverse an integer recursively.

**JAVA CODE**

package SimplerPrograms; public class SumofDigits { int Sum = 0, Reminder; public int SumoftheDigits(int Number) { if(Number > 0) { Reminder = Number % 10; Sum = Sum + Reminder; SumoftheDigits(Number / 10); return Sum; } else { return 0; } } }

Within the Main program of Java sum of digits of a number, we will create an instance of the above specified class and call the methods

package SimplerPrograms; import java.util.Scanner; public class SumofDigitsUsingRecursion { 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: "); Number = sc.nextInt(); SumofDigits sd = new SumofDigits(); Sum = sd.SumoftheDigits(Number); System.out.format("\n Sum of the digits of Given Number = %d", Sum); } }

**OUTPUT**

**ANALYSIS**

**SumofDigits Class Analysis:**

Within this Java sum of digits program example, In this class, we defined a function. Following function will accept integer value as parameter value and return integer value

public int SumoftheDigits(int Number) {

Lets 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 compiler will return the Sum of digits in a given number. Here, we are using
statement inside the If block because it will help to call the function Recursively with 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**SumoftheDigits(Number / 10);** - If the condition is False, then compiler will return the Sum as Zero

**NOTE:** For Recursive functions it is very important to place a condition before using the function recursively otherwise, we will end up in infinite execution (Same like infinite Loop).

Please be careful :)

**Sum of Digits Main Class Analysis:**

In the main class of Java sum of digits of a number program, First, we created an instance / created an Object of the * SumofDigits* Class

SumofDigits sd = new SumofDigits();

Next, we are calling the *Sumofthe Digits(Number)* method. As we all know that the method will return integer value so, we are assigning that return value to Sum

Sum = sd.SumoftheDigits(Number);