JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Check Disarium Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, exploring mathematical patterns is a fascinating endeavor. One such numeric pattern is the Disarium number.
A Disarium number is a number defined by the sum of its digits each raised to the power of its respective position.
In this tutorial, we'll delve into a JavaScript program designed to check whether a given number is a Disarium number.
π Example
Let's explore the JavaScript code that checks whether a given number is a Disarium number.
// Function to count the number of digits in a given number
function countDigits(number) {
return String(number).length;
}
// Function to check if a number is a Disarium number
function isDisarium(number) {
const originalNumber = number;
let digitCount = countDigits(number);
let sum = 0;
while (number !== 0) {
const digit = number % 10;
sum += Math.pow(digit, digitCount);
digitCount--;
number = Math.floor(number / 10);
}
return sum === originalNumber;
}
// Driver program
// Replace this value with your desired number
const inputNumber = 89;
// Check if the number is Disarium
if (isDisarium(inputNumber)) {
console.log(`${inputNumber} is a Disarium number.`);
} else {
console.log(`${inputNumber} is not a Disarium number.`);
}
π» Testing the Program
To test the program with different numbers, replace the value of inputNumber in the code.
89 is a Disarium number.
Run the script to check if the number is a Disarium number.
π§ How the Program Works
- The program defines a function countDigits to count the number of digits in a given number.
- The isDisarium function checks if a number is a Disarium number by summing the digits each raised to the power of its position.
- The driver program tests the isDisarium function with a sample number (replace it with your desired number).
π Between the Given Range
Let's explore the JavaScript code that checks for Disarium numbers in the specified range.
// Function to check if a number is a Disarium number
function isDisariumNumber(num) {
const numStr = num.toString();
let sum = 0;
for (let i = 0; i < numStr.length; ++i) {
sum += Math.pow(parseInt(numStr[i]), i + 1);
}
return sum === num;
}
// Function to find Disarium numbers in the range 1 to 100
function findDisariumNumbersInRange() {
const disariumNumbers = [];
for (let i = 1; i <= 100; ++i) {
if (isDisariumNumber(i)) {
disariumNumbers.push(i);
}
}
return disariumNumbers;
}
// Driver program
const disariumNumbersInRange = findDisariumNumbersInRange();
console.log("Disarium numbers in the range 1 to 100:");
console.log(disariumNumbersInRange.join(" "));
π» Testing the Program
Disarium numbers in the range 1 to 100: 1 2 3 4 5 6 7 8 9 89
Run the script to see the Disarium numbers in the range from 1 to 100.
π§ How the Program Works
- The program defines a function isDisariumNumber that checks if a given number is a Disarium number based on the defined condition.
- It also defines a function findDisariumNumbersInRange that iterates through numbers from 1 to 100 and identifies Disarium numbers.
- The main section calls these functions and prints the Disarium numbers in the specified range.
π§ Understanding the Concept of Disarium Numbers
Before we dive into the code, let's grasp the concept of Disarium numbers.
A Disarium number is a number such that the sum of its digits, each raised to the power of its position, equals the number itself.
For example, the number 89 is a Disarium number because 8^1 + 9^2 equals 89.
π Conclusion
Understanding and implementing programs to identify numeric patterns, such as Disarium numbers, is a valuable skill in the world of programming.
The provided javascript program offers a practical example of checking whether a given number follows the Disarium pattern.
Feel free to use and modify this code for your specific use cases. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Check Disarium Number), please comment here. I will help you immediately.