WAP For Sum Until the Sum Of All Digits is a Single Digit

WAP For Sum Until the Sum Of All Digits is Single Digit: The program InterviewPrograms53 aims to find the digital root of an integer. The digital root of a number is the single-digit value obtained by repeatedly adding all its digits until the result has only one digit.

This type of problem is also known as “Digital Root.” We are trying to give different types of solutions for every program, and out of those, you can implement as per your requirement.

Sum Until the Sum Of All Digits is a Single Digit

package com.softwaretestingo.interviewprograms;
import java.util.Arrays;
import java.util.stream.IntStream;
public class InterviewPrograms53
{
	/*
	 * Given an integer num, repeatedly add all its digits until the result has only
	 * one digit, and return it.
	 * 
	 * Example 1:
	 * Input: num = 38
	 * Output: 2
	 * 
	 * Explanation: The process is
	 * 38 --> 3 + 8 --> 11
	 * 11 --> 1 + 1 --> 2 
	 * Since 2 has only one digit, return it.
	 * Example 2:
	 * Input: num = 0
	 * Output: 0
	 */

	public static void main(String[] args) 
	{
		String input1 = "38";
		String input2 = "1485631";
		String input3 = "0";
		System.out.println(input1+": "+add(input1));
		System.out.println(input2+": "+add(input2));
		System.out.println(input3+": "+add(input3));
	}
	public static int add(String input) 
	{
		int[] array = Arrays.asList(input.split("")).stream().mapToInt(Integer::parseInt).toArray();
		int sum = IntStream.of(array).sum();
		if (sum / 10 == 0)
		{
			return sum;
		} 
		else 
		{
			return add(String.valueOf(sum));
		}
	}
}

Output

38: 2
1485631: 1
0: 0

Here’s how the program works:

  1. The program takes three input numbers: 38, 1485631, and 0.
  2. For each input number, it converts the number into an array of integers. For example, “38” becomes [3, 8].
  3. It then calculates the sum of all the digits in the array using IntStream.of(array).sum(). For “38”, the sum would be 3 + 8 = 11.
  4. If the sum has more than one digit (sum / 10 is not equal to 0), the program recursively calls the add function with the sum as a string until the final digital root is obtained.
  5. For “38”, the process continues with the sum 11, which becomes 1 + 1 = 2. Since 2 has only one digit, the program returns it as the digital root of 38.
  6. The program prints the digital roots of all three input numbers to the console.

Alternative 2

This Java program aims to solve a mathematical problem where we need to repeatedly add all the digits of an input number until the result becomes a single-digit number. Let’s explain the program step by step for Java beginners:

In the below program, we have used the While Loop and If else statements.

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms53_1
{
	/*
	 * Given an integer num, repeatedly add all its digits until the result has only
	 * one digit, and return it.
	 * 
	 * Example 1:
	 * Input: num = 38
	 * Output: 2
	 * 
	 * Explanation: The process is
	 * 38 --> 3 + 8 --> 11
	 * 11 --> 1 + 1 --> 2 
	 * Since 2 has only one digit, return it.
	 * Example 2:
	 * Input: num = 0
	 * Output: 0
	 */

	public static void main(String[] args) 
	{
		int input1 = 38;
		int input2 = 1485631;
		int input3 = 0;
		System.out.println(input1+": "+add(input1));
		System.out.println(input2+": "+add(input2));
		System.out.println(input3+": "+add(input3));
	}
	public static int add(int num) 
	{
		//variable to store sum of digits  
		int sum = 0;  
		//loop to do sum while sum is not less than or equal to 9  
		while (num > 0 || sum > 9)  
		{  
			if (num == 0)   
			{  
				num = sum;  
				sum = 0;  
			}  
			//determines the last digit of the number and add that digit to the sum variable   
			sum = sum + num % 10;  
			//remove the last digit of the number  
			num = num / 10;  
		}  
		//returns the number   
		return sum;  
	}
}

Output:

38: 2
1485631: 1
0: 0
  1. Inside the main method, three test cases are set up with different input numbers (input1, input2, and input3).
  2. The add method is called for each test case, and the result is printed to the console using System.out.println.
  3. The add method takes an integer num as input and returns an integer as output. It uses a loop to repeatedly add the digits of num until the sum becomes a single-digit number.
  4. Inside the add method, a variable sum is declared to store the sum of digits.
  5. The while loop runs as long as num is greater than 0 or sum is greater than 9. The loop condition ensures that we continue the summing process until we get a single-digit result.
  6. Inside the loop, the last digit of num is determined using the expression num % 10, and this digit is added to the sum.
  7. The last digit of num is removed by dividing num by 10 (num = num / 10), which effectively removes the rightmost digit.
  8. The loop continues until num becomes 0, at which point, we assign the current value of sum to num and reset sum to 0, so that we can continue summing its digits.
  9. Once the loop terminates, the add method returns the final value of sum, which represents the single-digit result.
  10. The program then prints the input number and its corresponding single-digit result for each test case.

In summary, this Java program takes an input number, repeatedly adds its digits until it becomes a single-digit number, and returns that single-digit result. It demonstrates how to use loops and basic arithmetic operations to solve a mathematical problem.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment