Last Updated on May 24, 2023 by Prepbytes
One of the most fundamental programs is to add the digits of a number. The idea at play here aids in comprehending how loops work. The simplest way to find the sum of a number’s digits is to split the number into digits and add each one separately in integer form. In Java, there are various methods for calculating a number’s digit sum. These techniques will be covered in this article.
Steps to Find the Sum of Digits of a Number in Java
Enter any integer (or just take the number on its own). The sum of the number’s digits will then be determined by using the modulus and division procedures. Let’s examine the procedures.
- First, we assign the number as the initial value of the variable n. We start a variable called sumOfDigits with a value of 0.
- The last digit of the integer will be revealed when we use the modulo operator to calculate the remainder.
- The received digit (remainder) will then be continuously added to the sumOfDigits variable.
- In order to eliminate the last digit of the number, we must additionally divide the number n by 10.
- Up until the number n equals 0, repeat steps 2-4.
Approaches to find Sum of Digits of a Number in Java
Here, we will discuss three different approaches to calculate the sum of digits of a Number in Java.
Approach 1: Sum of Digits of a Number in Java By using for loop
The sumOfDigits is increased by the remaining n/10 after the for loop iterates until n!=0 is false. The for loop is broken and the sum is reported if n=0.
Code Implementation
import java.util.Scanner; class PrepBytes { public static void main(String arg[]) { long n, sumOfDigits; n = 23; for(sumOfDigits = 0 ; n!=0 ; n/=10) { sumOfDigits += n%10; // adding remainder(last digit) to the sum } System.out.println(sumOfDigits); // printing result } }
Output
5
Approach 2: Sum of Digits of a Number in Java By using Function
By enclosing our code in a function, we can also make it more organized and modular. We don’t need to repeatedly write its implementation because the function can be called from anywhere and will always return the sum of the digits.
Code Implementation
class PrepBytes{ // sum function returns the sum of digits static long sum(long n) { long sumOfDigits = 0; // while the remaining number not becomes 0 while (n != 0) { sumOfDigits = sumOfDigits + n % 10; // adding last digit to sum n = n/10; // removing last digit } return sumOfDigits; // returning sum of digits } public static void main(String args[]) { long n; n = 23; System.out.println(sum(n)); // calling sum function for n } }
Output
5
Approach 3: Sum of Digits of a Number in Java By using Recursion
Recursion can also be used to calculate a number’s digit sum. Here, the presumption n==0 will be used. Until the initial condition is satisfied, the function will keep calling itself. Recursively calculating the remaining sum, the result from the remaining number is used each time the last digit is removed. The ternary operator is used in the programme below to write the aforementioned conditions. Run the code following the colon (:) in the ternary operator if the number is not zero; else, output 0. Recursively, the function sumOfDigits() is invoked.
Code Implementation
import java.util.*; class PrepBytes { static long sumOfDigits(long n) { // n == 0 is base condition return n == 0 ? 0 : n%10 + sumOfDigits(n/10) ; // calling function recursively } public static void main(String[] args) { long n; Scanner scn = new Scanner(System.in); n = 23; System.out.println(sumOfDigits(n)); // calling sumOfDigits fucntion } }
Output
5
Conclusion
- A number’s digits are added by dividing it into its component parts and adding each one separately to produce the final result.
- We covered three approaches in Java to compute the sum of a number’s digits: for loop, function, and recursion.
FAQ related to Sum of Digits of a Number in Java
Q1: What happens if I enter a negative number?
Ans. The sum of digits calculation will still work for negative numbers. The sum will be calculated by considering the absolute values of the digits. For example, the sum of digits for -123 will be the same as the sum of digits for 123, which is 6.
Q2: Can I calculate the sum of digits for a decimal number?
Ans. No, the sum of digits calculation is typically used for integer numbers. If you need to calculate the sum of digits for a decimal number, you would need to convert it to an integer first by rounding or truncating the decimal part.
Q3: What is the "Sum of Digits of a Number" in Java?
Ans. The "Sum of Digits of a Number" refers to the calculation of the sum of all the individual digits of a given number.