Check if a Number is Harshad Number using the Iterative Approach
Iterate through the digits of the number, sum them up, and then check if the original number is divisible by this sum.
Example: The below code example Uses the simple iteration Method to Check Whether a Number is a Harshad Number in JavaScript.
Javascript
function isHarshad(number) { let sum = 0; let num = number; while (num > 0) { sum += num % 10; num = Math.floor(num / 10); } return number % sum === 0; } console.log(isHarshad(15)); |
Output
false
JavaScript Program to Check Whether a Number is Harshad Number
A Harshad number (also called Niven number) is a number that is divisible by the sum of its digits. In other words, if you take a number, sum up its digits, and if the original number is divisible by that sum, then it’s a Harshad number. For example, 18 is a Harshad number because the sum of its digits is 1 + 8 = 9, and 18 is divisible by 9.
Below are the approaches to check if a number is a Harshad number or not using JavaScript:
Table of Content
- Iterative Approach
- Using inbuilt functions
- Using recursion
Contact Us