×

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem.

Example

Consider the case where we need to calculate the sum for the number 534636 so that it can be reduced to a single digit.

5+3+4+6+3+6 = 27

We'll also include numbers 2 and 7.

2 + 7 = 9

The result is 9, which is a single digit.

Brute Force Approach

  • The total number of digits is calculated.
  • the sum into a recursive call.
  • Return the number if it is less than 10.
  • Let's put the reasoning above into a Java application.

Java Program1 for single digit number

import java . util . * ;  
public class SingleDigitNumber1  
{   
static int digitSum ( int number  )  
{  
int sum1 = 0 ;  
while ( number > 0 || sum1 > 9 )  
{  
if ( number == 0 )   
{  
number = sum1 ;  // assigning sum1 to number
sum1 = 0 ;  // assigning 0 to sum1
}  


sum1 = sum1 + number % 10 ;  


number = number / 10 ;  
}  


return sum1 ;  
}  


public static void main ( String args [ ] )  
{  
int number = 534636 ;  
System . out . println ( " The sum of digits is : "+ digitSum ( number ) ) ;  
}  
}  

Output

Java Program to Add Digits Until the Number Becomes a Single Digit Number

The above-mentioned issue can also be resolved without the use of loops and recursion. This is how it's done:

This method starts by determining if the number can be divided by nine or not.Put the digits together if the number is divisible to see if the sum is 9-divisible or not. The integer is divisible by nine if it is; otherwise, it is not.

27 is an example where (2 + 7) = 9. The result of adding the numerals is 9. The total is, therefore, divisible by 9.

The digits of a number (n) that is divisible by nine should be added together until they equal one digit, which is always nine. For instance:

n = 3762

Number sum: 3 + 7 + 6 + 2 Equals 18.

A single digit number can be created by adding the number 18 further.

18 = 1 + 8 = 9

If a number has the form 9x, the answer is always 9. Whenever an integer has the form 9x+k

The shape of a number can be 9x or 9x + k. The response in the first scenario is always 9. The answer to the second example is always k, which is the remainder.

  • Return 0 if number is 0.
  • Find the number's remainder using 9. (number%9).
  • If the remaining is 0, give 9, else give the remainder.
if number == 0  
   return 0;  
if number % 9 == 0        
    digitSum(number) = 9  
else                 
    digitSum(number) = number % 9 




SingleDigitNumber2.java
import java . io . * ;  
public class SingleDigitNumber2   
{  
static int digitSum ( int number )  
{  
if (number == 0)  
return 0;  
return (number % 9 == 0) ? 9 : (number % 9);  
}     
public static void main (String s[])  
{  
int number = 534636;  
System.out.println("The sum of digits is: "+digitSum(number));  
}  
}  

Output

Java Program to Add Digits Until the Number Becomes a Single Digit Number

SingleDigitNumber3.java

import java.util.Scanner;
public class  SingleDigitNumber3{
   public static int sum_of_digits(int num) {
      int ld = 0;
      int sum1 = 0;
      while(num != 0) {
         ld = num % 10;
         sum1 = sum1 + ld;
         num = num / 10;
      }     
      return sum1;
  }
  public static int digitSum(int num) {
     int r = num;
     while(r / 10 != 0) {
   r = sum_of_digits(r);
     }
     return r;
  }


  public static void main(String[] args) {
      int num = 0;
      int d = 0;
      Scanner s =  new Scanner(System.in);      
      System.out.print("Enter an  number: ");
      num = s.nextInt();
      d = digitSum(num);
      System.out.println("The sum of digits until" +
         " single digit of the number "+num+
         " = "+d);
      s.close();
  }
}

Output

Java Program to Add Digits Until the Number Becomes a Single Digit Number

SingleDigitNumber4.java

import java.util.Scanner ;
public class SingleDigitNumber4 {
   public static int digitSum ( int num ) {
      if ( num == 0) return 0 ;
      else if ( num % 9 == 0 ) 
        return 9 ;
      else
        return num % 9 ;
   }
   public static void main ( String [ ] args ) {
      int num = 0 ;
      int d = 0 ;
      Scanner s =  new Scanner ( System . in ) ;
      System . out . print ( " Enter an  number : " ) ;
      num = s . nextInt ( ) ;
      d = digitSum ( num ) ;
      System . out . println("The sum of digits until" +" single digit of the number "+num+" = "+d);
      s . close ( ) ;
  }
}

Output:

Java Program to Add Digits Until the Number Becomes a Single Digit Number

Another simultaneous solution to the above problem is

public static int digitalSum ( int number )   
{  
return 1 + (number - 1) % 9 ;  
}  

Related Topics

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Java Code Coverage Tools

Code coverage testing is a crucial metric that gauges how thoroughly the program's source code has been tested. The market is flooded with Code Coverage Tools, making it difficult to...

7 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.