# 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

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

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

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:

Another simultaneous solution to the above problem is

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