JavaScript Program to Check Whether a Number is Harshad Number
Last Updated :
08 Mar, 2024
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:
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));
Check if a Number is Harshad Number using inbuilt functions
Utilize mathematical properties to directly calculate whether the number is a Harshad number without iterating through its digits.
Example: The below code example Uses the simple inbuilt Method to Check Whether a Number is Harshad Number in JavaScript.
JavaScript
function isHarshad(number) {
let sumOfDigits = number
.toString()
.split("")
.reduce((acc, digit) => acc + parseInt(digit), 0);
return number % sumOfDigits === 0;
}
console.log(isHarshad(15));
Check if a Number is Harshad Number using recursion
In this approach, we'll recursively sum up the digits of the number until we get a single-digit number. Then, we'll check if the original number is divisible by this sum. If the original number is divisible by the sum of its digits, it's a Harshad number.
Example: The below code example Uses recursion Method to Check Whether a Number is Harshad Number in JavaScript.
JavaScript
function sumOfDigits(number) {
if (number < 10) {
return number;
} else {
return (number % 10) + sumOfDigits(Math.floor(number / 10));
}
}
function isHarshad(number) {
let sum = sumOfDigits(number);
return number % sum === 0;
}
console.log(isHarshad(15));
Similar Reads
JavaScript Program to Check for Palindrome Number We are going to learn about Palindrome Numbers in JavaScript. A palindrome number is a numerical sequence that reads the same forwards and backward, It remains unchanged even when reversed, retaining its original identity. Example: Input : Number = 121Output : PalindromeInput : Number = 1331Output :
4 min read
JavaScript Program to Check whether the Input Number is a Neon Number A neon number is a number where the sum of digits of its square equals the number itself. For example, 9 is a neon number because 9^2 = 81, and the sum of the digits 81 is 9. There are several ways to check if a given number is a neon number in JavaScript or not which are as follows: Table of Conten
2 min read
Check Whether a Number is Strong Number in JavaScript Strong Numbers are the numbers whose sum of the factorial of digits is equal to the original number. In JavaScript, we can check whether the number is a strong number or not using various approaches. In this article, we will explore all the possible approaches with their practical implementation. Th
3 min read
Python Program to Check If a Number is a Harshad Number Harshad Numbers can be divided by the sum of its digits. They are also called Niven Numbers. For instance, 18 is a Harshad Number as it can be divided by 9, the sum of its digits (8+1=9). In this article, we will discuss various approaches to determine whether the given number is a Harshad Number in
2 min read
JavaScript Program to Check if a Number is Float or Integer In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read