Sum up a number until it becomes one digit - JavaScript


In the given problem statement we are presented with a number and we have to sum up the given number until it becomes one digit and implement the code in Javascript for getting the required sum.

Understanding the Problem

In the given program we will have a number and our task is to repeatedly sum its digits until the outcome of the number becomes a single digit. For example suppose we have a number like 874563 and we need to add all of its digits (8 + 7 + 4 + 5 + 6 + 3 = 33), and then again sum the digits of the first result (3 + 3 = 6). The final answer is 6 which is a single digit for the given number.

Logic for the given Problem

In the function we will have a number as input and the task of this function will repeatedly sum its digits until the result becomes one digit. The function will use a while loop to check that the number is greater than or equal to 10. If it is greater than 10 then we will use while loop to calculate the sum of individual digits.Then we will use a variable to store the sum and after adding the digit to the sum the number will be divided by 10 and we will round down with the help of Math.floor function to remove the last digit. At the end we will finally return the single digit as the outcome.

Algorithm

Step 1: Declare a function and give it a name as sumToOneDigit which accepts a parameter as a number. This function will calculate the sum and convert the given number into a single digit.

Step 2: Inside the above function we will use a while loop to check that the given number is greater than or equal to 10. This means we will keep iterating the number until we obtain a single digit as a result.

Step 3: If the condition that the number is greater than 10 then we will create a variable to store the sum and set initially to 0.

Step 4: With the usage of nested while loop we will calculate the sum of the individual digits and inside this loop we will add the last digit of the number to the sum variable and this step will give us a remainder when dividing it by 10 which will extract the last digit.

Step 5: Return the value of the number which will be a single digit as the required result.

Example

//Sum until the number is reduced to one digit
function sumToOneDigit(num) {
  while (num >= 10) {
   let sum = 0;
   while (num > 0) {
     sum += num % 10;
     num = Math.floor(num / 10);
   }
   num = sum;
  }
  return num;
}

const num = 987654321;
const result = sumToOneDigit(num);
console.log(result);

Output

9

Complexity

In the code we are using two while loops and while loop has logarithmic time complexity, so the time complexity of this code is O(log n), where n is the size of the input number. And the space acquired by the code is O(1) because the amount of memory consumed is independent of the input size.

Conclusion

In the code we have solved the given problem of adding the digits of the given integer number until the number is not reached to a single digit.

Updated on: 16-Aug-2023

626 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements