Java Program to Add Digits Until the Number Becomes a Single Digit Number17 Mar 2025 | 3 min read In this section, we will create Java programs that add digits of a number until the number becomes a single-digit number. The problem is also known as the digit root problem. ![]() Example Suppose, 76345 is a number for which we have to find the sum until it becomes a single-digit number. 7 + 6 + 3 + 4 + 5 = 25 Further, we will add 2 and 5. 2 + 5 = 7 We get 7 as a single-digit number. Brute Force Approach
Let's implement the above logic in a Java program. SingleDigit.java Output: The sum of digits is: 1 We can also solve the above problem without using any loop and recursion. The approach is as follows: In this approach, first, check if the number is divisible by 9 or not. If divisible, add the digits together and check if the sum is divisible by the number 9 or not. If divisible, the number is divisible by 9, else not. For example, 54 i.e. (5 + 4 = 9). We get 9 as the sum of digits. Hence, the sum is divisible by 9. We observe that if a number (n) is divisible by 9, then sum up its digit until the sum becomes a single digit which is always 9. For example: n = 2880 Sum of digits = 2 + 8 + 8 + 0 = 18 The number 18 further can be added to form a single digit number. 18 = 1 + 8 = 9 The answer is always 9, if a number has the form like 9x. If the number is of the form 9x+k A number can be of the form 9x or 9x + k. For the first case, the answer is always 9. For the second case, the answer is always k which is the remainder.
Let's implement the above approach in a Java program. SumOfDigits.java Output: The sum of digits is: 2 The above approach solves the problem in O(1) time. Another approach that solves the problem in O(1) is as follows: |
Hypertext Transfer Protocol (HTTP) supports many methods to do any task on the server or to receive any data from a server. The Java Get and Post methods are two prominent methods of HTTP for sending and receiving data from a server. Even though both methods...
6 min read
? In the realm of Java programming, the Eclipse Integrated Development Environment (IDE) stands as a reliable and feature-packed tool for software development. One of the key strengths of Eclipse lies in its adaptability, allowing developers to integrate external libraries into their projects seamlessly. JAR Files JAR files are...
3 min read
In Java, Collection is a framework that belongs to java.util package. It provides classes and interfaces to manipulate a group of objects. Java offers various collection classes like ArrayList, LinkedList, HashSet, and TreeSet, etc. In this section, we are going to write a Java program to get...
4 min read
In this section, we will learn what is jumping number and also create Java programs to check if the given number is a jumping number or not. The jumping number program frequently asked in Java coding tests and academics. Jumping Number A number N called a jumping number...
7 min read
There are various scenarios where we need to consider case sensitivity. The ability to ignore case while comparing strings can be crucial in many applications. In this section, we will explore how to perform case-insensitive string comparisons in Java, using the power of the equalsIgnoreCase() method....
5 min read
In Java especially in applications, managing time as well as date is one of those tasks that is very common. JDK 8 contains time package that includes the set of classes for working with the time and date of these, the LocalTime class is particularly created...
5 min read
An array that either has no elements or all of its elements are null is referred to as an empty array in Java. The new keyword in Java can be used to declare an empty array. An array that is empty or has no elements is...
3 min read
Java is the most popular object-oriented programming language but it has some drawbacks. The major drawback is to write lots of boilerplate code. To overcome this drawback, project Lombok comes into existence. It is a tool that spices up our Java application. In this section,...
13 min read
Java's basic data structure, HashMap, enables programmers to store and retrieve data effectively. Nesting of HashMap is a useful notion when working with complex data structures. In this section, we will discuss nested HashMap, its benefits, and implementation in applications. Comprehending and Applying a Map of...
5 min read
Following is the program to demonstrate it. File: ConvertStringToInteger.java public class ConvertStringToInteger { public static void main(String[] args) { //1st way String str1 = "5"; int result = Integer.parseInt(str1); // Using Integer.parsrInt() System.out.println(result); //2nd way String str2 = "5"; Integer result2 =...
1 min read
We request you to subscribe our newsletter for upcoming updates.
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