➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
In this post, we will find the sum of digits until the single digit in Java. Previously we have developed a Java program to find the sum of digits of the number. But now we will find the sum of digits until the number becomes single digit,
Example:- number = 123456
=> The sum of digits of 123456 = 1+2+3+4+5+6 = 21
=> The number 21 is of two digits number so again we will find the sum of digits of the number,
=> The sum of digits of 21 = 2+1 = 3
Now, 3 is single-digit so it is the digital sum of the number 123456.
import java.util.Scanner;
public class DigitalSum {
// method to find sum of digits
// of a given number
public static int sumOfDigits(int number) {
// declare variables
int lastDigit = 0;
int sum = 0;
// loop to repeat the process
while(number != 0) {
// find last digit
lastDigit = number % 10;
// add last digit to sum
sum = sum + lastDigit;
// remove last digit
number = number / 10;
}
// sum value is the sum of digits
// of the given number
return sum;
}
// method to find digital sum
public static int digitalSum(int number) {
int result = number;
while(result / 10 != 0) {
result = sumOfDigits(result);
}
return result;
}
public static void main(String[] args) {
// declare variables
int number = 0;
int sumOfDigitsUntilSingle = 0;
// create Scanner class object
// for reading the values
Scanner scan = new Scanner(System.in);
// read inputs
System.out.print("Enter an integer number:: ");
number = scan.nextInt();
// find sum of digits of number
sumOfDigitsUntilSingle = digitalSum(number);
// display result
System.out.println("The sum of digits until" +
" single digit of the number "+number+
" = "+sumOfDigitsUntilSingle);
// close Scanner class object
scan.close();
}
}
The output of the different test-cases are:-
Enter an integer number:: 123456
The sum of digits until single digit of the number 123456 = 3
Enter an integer number:: 456
The sum of digits until single digit of the number 456 = 6
Enter an integer number:: 100
The sum of digits until single digit of the number 100 = 1
Efficient way to find sum of digits until single digit in Java
The Sum of digits until single digit in Java also can be calculated by directly dividing the number by 9. If number is divisible by 9 then it’s Sum of digits until single digit is 9 else it is number % 9
import java.util.Scanner;
public class DigitalSum {
// method to find digital sum
public static int digitalSum(int number) {
if(number == 0) return 0;
else if(number % 9 == 0)
return 9;
else
return number%9;
}
// main method
public static void main(String[] args) {
// declare variables
int number = 0;
int sumOfDigitsUntilSingle = 0;
// create Scanner class object
// for reading the values
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter an integer number:: ");
number = scan.nextInt();
// find sum of digits of number
sumOfDigitsUntilSingle = digitalSum(number);
// display result
System.out.println("The sum of digits until" +
" single digit of the number "+number+
" = "+sumOfDigitsUntilSingle);
// close Scanner class object
scan.close();
}
}
Output:-
Enter an integer number:: 123456
The sum of digits until single digit of the number 123456 = 3
The time complexity of this solution is O(1).
Using mathematical formula congruence, We can implement the another method in O(1)
public static int digitalSum(int number) {
return 1 + ( number - 1) % 9 ;
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
public static int digitalSum(int number) {
if(number == 0) return 0;
else if(number % 9 == 0)
return 9; //(9 should be returned instead of 0)
else
return number%9;
}
Thanks, Shreya for improving the program. I have edited it.
Using mathematical formula congruence,
We can implement the 2nd method O(1) in one linear in Java,
public static int digitalSum(int number) {
return 1+ ( number – 1) % 9 ;
}