# Sum of Digits in Java

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. Then it will divide the given number into individual digits and adding those individuals (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);
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);
}
}```

In this Java sum of digits program, first, We declared three integer variables Number, Remainder, and Sum = 0. The following statements will ask the user to enter any positive integer. Then, that number 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, the 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, 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

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 = Number % 10
Reminder = 987 % 10 = 7

Sum = Sum + Reminder
Sum = 6 + 7 = 13

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  Number = 98 and Sum = 13. It means, While (98 > 0) is TRUE

Reminder = Number % 10
Reminder = 98 % 10 = 8

Sum = Sum + Reminder => 13 + 8
Sum = 21

Number = Number / 10 => 98 / 10
Number = 9

Fourth Iteration

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

Reminder = Number % 10
Reminder = 9 % 10 = 9

Sum = Sum + Reminder
Sum = 21 + 9 = 30

Number = Number / 10 => 9 / 10
Number = 0

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

The last Java System.out.format statement will print the Sum variable as the output. So, the output of the 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 the sum of digits in java allows the user to enter any positive integer. Then it will divide the given number into individual digits and adding those individuals (Sum) digits using Java For Loop.

```// 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);
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. 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 Java sum of digits program will divide the given number into individual digits and adding those individuals (Sum) digits using the Java For Loop

```// 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);
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;
}
}```
``````Please Enter any Number:
3698
Sum of the digits of Given Number = 26``````

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 the main() program, the compiler will immediately jump to below function:

`public static int SumofDigits(int Number) {`

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

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

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

In this sum of digits in 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 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 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);
}
}```
``````Please Enter any Number:
456789
Sum of the digits of Given Number = 39``````

SumofDigits Class Analysis:

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

`public int SumoftheDigits(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 SumoftheDigits(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.

Sum of Digits Main Class Analysis:

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

`SumofDigits sd = new SumofDigits();`

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

`Sum = sd.SumoftheDigits(Number);`