- JavaScript Programs
- JavaScript Programs
- JavaScript Add Two Numbers
- JS Add Subtract Multiply Divide
- JavaScript Check Even/Odd
- JavaScript Add n Numbers
- JavaScript Add Number's Digit
- JavaScript Check Leap Year
- JavaScript Check Prime Number
- JavaScript Check Vowel or Not
- JavaScript Tutorial
- JavaScript Tutorial
- Computer Programming
- Learn Python
- Python Keywords
- Python Built-in Functions
- Python Examples
- Learn C++
- C++ Examples
- Learn C
- C Examples
- Learn Java
- Java Examples
- Learn C#
- Learn Objective-C
- Web Development
- Learn HTML
- Learn CSS
- Learn SQL
- Learn PHP
JavaScript Program to Add Digits of a Number
In this article, you will learn and get code to add digits of a number in JavaScript. Here are the list of JavaScript programs, you will go through:
- Add digits of number in JavaScript without user input
- Allow user to enter the number. Live output of this program is given
Add Digits of a Number in JavaScript
This program does not allows user to enter the data. That is, this program only adds all the digits of a number say 1234 and prints its addition result on output.
This is just a simple JavaScript code that reminds you, how the addition of all digits of a number gets performed
<!doctype html> <html> <body> <script> var num=1234, rem, sum=0; while(num) { rem = num%10; sum = sum+rem; num = Math.floor(num/10); } document.write(sum); </script> </body> </html>
Save this code in a file with .html extension and open it in a web browser. Here is the output:
Note - The Math.floor() method rounds the number to its nearest integer. That is, Math.floor(12/10) gives you 1, instead of 1.2
The following code:
while(num) { rem = num%10; sum = sum+rem; num = Math.floor(num/10); }
works in a way that:
- The condition, of while loop evaluates to be true, because the value of num is not 0, initially
- So program flow goes inside the loop
- There num%10 or 1234%10 or 4 gets initialized to rem. So rem=4
- And sum+rem or 0+4 or 4 gets initialized to sum. So sum=4
- The final statement, Math.floor(num/10) or Math.floor(1234/10) or Math.floor(123.4) or 123 gets initialized to num
- Now again the condition of while loop gets evaluated. Because the value of num is 123 which is not equal to 0, therefore again the condition evaluates to be true
- So program flow again goes inside the loop and evaluates all the three statements
- This process continues until the condition of while loop evaluates to be false
The document.write() method writes the value of sum into an HTML output.
Get Input from User
This program allows user to enter the number, then find and prints the sum of digits of that entered (given) number as shown here:
<!doctype html> <html> <head> <script> function addDigitsOfNumber() { var num, rem, sum=0; num = parseInt(document.getElementById("num").value); while(num) { rem = num%10; sum = sum+rem; num = Math.floor(num/10); } document.getElementById("result").value = sum; } </script> </head> <body> <p>Enter the Number: <input id="num"></p> <button onclick="addDigitsOfNumber()">Add Digits Of Number</button> <p>Result = <input id="result"></p> </body> </html>
Here is its sample output with user input, 1234:
Live Output of Previous Program
Here is the live output of above JavaScript program on addition of digits of given number by user:
Enter the Number:
Result =
After clicking on the button named Add Digits of Number, a function addDigitsOfNumber() gets called. Therefore, all the statements inside this function gets executed, and the value of input whose id is result gets changed with the value of sum.
« Previous Program Next Program »