Find the Sum of the Digits of a Number in Javascript Language

Find the Sum of the Digits of a Number in Javascript Language

We will discuss three methods to solve this problem using Javascript

Introduction

Hi everyone today we find the sum of N Natural Numbers in javascript. let's discuss how to solve this problem statement. if you want to check out yesterday's problem click here.

Our goal is to divide a given number into its component digits and add them all up given an input integer as the number. We'll utilize the following two operators to do this:

  • Method 1: Using Brute Force

  • Method 2: Using Recursion I

  • Method 3: Using Recursion II

  • Method 4: Using ASCII Table

Method 1: Using Brute Force

In JavaScript, we can use the modulus operator (%) to get the remainder of a division operation and Math.floor() function to get the integer part of a division operation. The while loop will continue as long as the value of num is not equal to 0. The loop body will add the remainder of num divided by 10 to the sum, and then divide num by 10 and assign the result back to num. Finally, the console.log() statement will output the value of the sum to the console.

Algorithm

  • step 1:Initialize the variable num to 12345 and the variable sum to 0.

  • step 2: While num is not equal to 0, perform the following steps: a. Calculate the remainder of num divided by 10 and add it to the sum. b. Divide num by 10 and assign the result back to num.

  • step 3: Print the value of the sum.

Javascript Code

let num = 12345;
let sum = 0;

while (num !== 0) {
sum += num % 10;
num = Math.floor(num / 10);
}

console.log("Sum of digits: " + sum);

Output

Sum of digits: 15

Method 2: Using Recursion I

Algorithm

  • step 1: Initialize the input number "num" to 12345 and "sum" to 0.

  • step 2: Call the function "getSum" with parameters "num" and "sum".

  • step 3: In the "getSum" function, check if the input number "num" is equal to 0.

  • step 4: If "num" is 0, return the sum.

  • step 5: If "num" is not 0, add the last digit of the number to the sum by taking the modulo of the number with 10 and adding it to the sum.

  • step 6: Remove the last digit of the number by dividing it by 10 and call the "getSum" function recursively with the new number and the updated sum.

  • step 7: The final returned sum is the sum of digits of the input number.

  • step 8: Print the result on the console.

Javascript Code

function getSum(num, sum) {
  if (num === 0) {
    return sum;
  }
  sum += num % 10;
  return getSum(Math.floor(num / 10), sum);
}

const num = 12345;
const sum = 0;
console.log("Sum of digits: " + getSum(num, sum));

Output

Sum of digits: 15

Method 3: Using Recursion II

Algorithm

  • step 1:Start

  • step 2:Initialize a variable "num" with the given number.

  • step 3: Initialize a variable "sum" with 0.

  • step 4: While the "num" is not equal to 0, repeat the following steps: a. Add the remainder of "num" divided by 10 to "sum". b. Divide "num" by 10 and update its value.

  • step 5: Return the value of "sum".

    • step 6: End.

Javascript Code

function getSum(num) {
  if (num === 0) {
    return 0;
  }
  return (num % 10) + getSum(Math.floor(num / 10));
}

const num = 12345;
console.log("Sum of digits: " + getSum(num));

Output

Sum of digits: 15

Conclusion

So this article we solved the problem statement using javascript language. I hope you found this article helpful for your web development journey.

  • If you fill this article informative please like this article, if you have any suggestions please comment on this article, share it on social media and follow me on

  • Hashnode - Gaurav Patil

  • Twitter - @GauravYPatil