In Java, to print the sum of digits of a given number we can use loop concepts such as while loop or for loop. If the input number is 1234, then we need to add every digit of that number like 1+2+3+4 and print the output as 10.
In this article we will learn writing program in java to print the sum of digits of a given number. We will also see examples, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We have to write a java program to print the sum of digits of a given number. The program should take integer as an input, and add each digit of that input integer one by one. And once all digit added, print the sum as output. This action should be done in our program.
Let’s see an example,
Example:
For input number 12, Output should be 3
Explanation:
Input integer number: 12
sum= 1+ 2 = 3
Hence, for the input number 12, it's sum of digits is 3.
Logic to print the sum of digits of a given number
To find the sum of input integer, First we will take last digit and add it to the sum variable (initially sum should be kept as 0 to get proper result).
After adding the digit to sum, we should reduce the number by dividing it to 10 for further iterations and should add the last digit in this updated number to the sum. And we should repeat this process until input number become to 0.
Once input number become 0, it means we have visited each digits of the input number
Java program to print the sum of digits in a given number
Program 1: Using while loop
In the below program we are using while loop to print the sum of digits in of a given number.
import java.util.Scanner;
public class SumDigits {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int num = s.nextInt();
int digit=0;
int sum = 0;
while (num != 0) {
digit=num %10; // gives last digit and stores in digit
sum+=digit;// adds digit to the sum
num=num/10;// remove last digit from number
}
System.out.println("sum of the digits: "+sum);
s.close();
}
}
Output:
Enter a number
1345
sum of the digits: 13
Java program explanation to print the sum of digits in a given number
- The program starts by importing the
Scanner
class for input reading. - Asks the user to enter a number.
- It reads and stores the number using
nextInt()
method ofScanner
. - Initializes
digit
andsum
variables to0
. - Enters a while loop that continues until
num
becomes0
. - Inside the loop:
- Computes
digit
as the last digit ofnum
usingnum % 10
. - Adds
digit
tosum
. - Updates
num
by removing its last digit usingnum / 10
. - After the loop completes (when
num
is0
), prints the sum of the digits. - Closes the
Scanner
object to release resources.
Program 2: Using for loop
In the below program we are using for loop to print the sum of digits of a given number in Java.
import java.util.Scanner;
public class SumDigits {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int num = s.nextInt();
int digit=0;
int sum = 0;
for (int i=num ;i != 0;i/=10) {
digit=i %10; // gives last digit and stores in digit
sum+=digit;// adds digit to the sum
}
System.out.println("sum of the digits: "+sum);
s.close();
}
}
Output:
Enter a number
23456
sum of the digits: 20
Java program explanation to print the sum of digits in a given number
- The program imports the
Scanner
class for input reading. - Asks the user to enter a number and reads it using
nextInt()
. - Initializes variables
digit
to store each digit andsum
to accumulate the sum of digits. - Uses a
for
loop to iterate untilnum
becomes0
. - Inside the loop:
- Computes
digit
as the last digit ofi
usingi % 10
. - Adds
digit
tosum
. - Updates
i
by removing its last digit usingi /= 10
. - After the loop completes (when
i
is0
), prints the sum of the digits. - Closes the
Scanner
object to release resources.
Conclusion
In this tutorial we have learnt writing the Java program to calculate the sum of the digits in a given number using while loop and for loop.
You can also check our awesome programs here
- Java Program to calculate power of a number without using pow method.
- Java Program to calculate power of a number using pow method.
- Java Program to check number is palindrome or not.
- Java Program to print Fibonacci series of a given number.
- Java Program to print Fibonacci series of a given number using recursion.
Happy Coding !!