• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP For Sum Until the Sum Of All Digits is a Single Digit

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

Last Updated on: July 23, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • 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.

    Factorial Number In Java
    Factorial Number In Java
    WAP to Print Highest ASCII Value Character From String
    WAP to Print Highest ASCII Value Character From String
    Palindrome Number In Java Example
    Palindrome Number In Java Example Program
    WAP to Split a String on Uppercase Characters
    WAP to Split a String on Uppercase Characters
    WAP to Verify Balanced Parentheses In Java
    WAP to Verify Balanced Parentheses In Java
    Substring Matching In Java
    Substring Matching In Java
    WAP For Removing White Spaces In A String
    WAP For Removing White Spaces In A String
    Passing Arrays To Functions In Java
    Passing Arrays To Functions In Java
    Find Duplicate Elements in Array
    Find Duplicate Elements in Array
    Swap Two Numbers In Java
    Swap Two Numbers In Java

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers