Sum of Digits of a Number in JavaIn this section, we will create Java programs to find the sum of digits of a number in Java. In order to find the sum of digits of a number, we must familiar with the Java loops and operators. Steps to Find the Sum of Digits of a Number in JavaEnter any integer number as input. After that, we use modulus and division operation respectively to find the sum of digits of the number as output. Let's see the steps.
Let's understand the above steps mathematically and find the sum of digits of a number. Suppose, we have to find the sum of digits of the number (N) 674. First iteration 674 % 10 = 4 Sum = 0 + 4 = 4 674 / 10 = 67 Second iteration 67 % 10 = 7 Sum = 4 + 7 = 11 67 / 10 = 6 Third iteration 6 % 10 = 6 Sum = 11 + 6 = 17 6 / 10 = 0 Now the number (N) has become 0. So, we will not process it further. Hence, we get 17 as the sum of digits of the number 674. Let's implement the above steps in a Java program. Java Programs to Find the Sum of Digits of a NumberThere are the following ways to find the sum of digits of a number:
Using While LoopIt is the simplest approach to find the sum of the digits of a number using a while loop. Let's break down the steps: Initialize Variables: number: It stores the user input. digit: It holds the last digit of the number. sum: It accumulate the sum of the digits. Loop Until the Number Becomes 0:
Print the Result. SumOfDigitExample1.java Output: Enter the number: 876 Sum of Digits: 21 Using for LoopThis approach uses a for loop to achieve the same result, which might be more concise in some cases. Initialize Variables: number: It stores the user input. sum: It accumulates the sum of the digits. Loop Until the Number Becomes 0: In each iteration, add the last digit to sum and remove the last digit from number. Print the Result. SumOfDigitsExample2.java Output: Enter a number: 3456788 Sum of digits: 41 Using FunctionEncapsulating the logic in a function improves code reusability and clarity. Define Function: The function findSum() takes an integer and returns the sum of its digits. Use while Loop Inside the Function: The logic inside the function is similar to the while loop approach. Call the function from main() method. SumOfDigitsExample3.java Output: The sum of digits: 26 In the following program, we have reduced the lines of code and implement the steps in a for loop only. SumOfDigitsExample4.java Output: The sum of digits: 35 Using RecursionRecursion provides an elegant way to solve this problem by repeatedly breaking it down into smaller sub-problems. Define Recursive Function: The function calls itself with a reduced number until the base case (number == 0) is reached. Base Case and Recursive Step: Base case: When number is 0, return 0. Recursive step: Add the last digit to the result of the recursive call with the remaining number. Here is the code for this approach: SumOfDigitsExample5.java Output: The sum of digits: 28 Using Recursion with an Instance VariableThis variant of the recursion method uses an instance variable to store the sum. Define Class with Instance Variable: Define a class with an instance variable sum. Recursive Function: The function updates the instance variable sum and calls itself until the number becomes 0. SumOfDigitsExample6.java Output: Enter a number: 983451 The sum of digits: 30 ConclusionEach approach has its own advantages:
Next TopicSum of Numbers in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India