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 Magic Number
Photo Credit to CodeToFun
π Introduction
In the world of programming, the concept of magic numbers is a fascinating one.
A magic number is a number in which the eventual sum of its digits, when the sum is recursively calculated, leads to a single-digit number that is 1. If the final sum is not 1, the number is not considered magic.
In this tutorial, we'll delve into a JavaScript program that checks whether a given number is a magic number or not.
π Example
Let's examine the JavaScript code that performs the magic number check.
// Function to check if a number is magic
function isMagicNumber(num) {
while (num > 9) {
let sum = 0;
// Calculate the sum of digits
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
num = sum;
}
// Check if the final sum is 1
return num === 1;
}
// Driver program
// Replace this value with your desired number
const number = 19;
// Check if the number is magic
if (isMagicNumber(number)) {
console.log(`${number} is a Magic Number.`);
} else {
console.log(`${number} is not a Magic Number.`);
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the code.
19 is a Magic Number.
Run the script to see whether the number is a magic number or not.
π§ How the Program Works
- The program defines a function isMagicNumber that takes an integer as input and checks if it is a magic number.
- Inside the function, it iteratively calculates the sum of the digits of the number until the number becomes a single-digit number.
- It then checks if the final sum is equal to 1, indicating a magic number.
π Between the Given Range
Let's take a look at the javascript code that checks for Magic Numbers within the specified range.
// Function to check if a number is a magic number
function isMagicNumber(num) {
while (num > 9) {
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
num = sum;
}
return num === 1;
}
// Driver program to find magic numbers in the range 1 to 50
function findMagicNumbersInRange(start, end) {
let magicNumbers = [];
for (let i = start; i <= end; i++) {
if (isMagicNumber(i)) {
magicNumbers.push(i);
}
}
return magicNumbers;
}
// Display magic numbers in the range 1 to 50
const magicNumbersInRange = findMagicNumbersInRange(1, 50);
console.log(`Magic Numbers in the range 1 to 50:\n ${magicNumbersInRange.join(' ')}`);
π» Testing the Program
Magic Numbers in the range 1 to 50: 1 10 19 28 37 46
Run the script to check for magic numbers in the range from 1 to 50.
π§ How the Program Works
- The program defines a function isMagicNumber that checks if a given number is a magic number.
- Inside the function, it uses a while loop to repeatedly sum the digits of the number until a single-digit result is obtained.
- The findMagicNumbersInRange function iterates through the range from 1 to 50 and identifies magic numbers using the isMagicNumber function.
- The program then displays the magic numbers in the specified range.
π§ Understanding the Concept of Magic Numbers
A magic number is a number in which the sum of its digits leads to a single-digit number, and that single-digit number is 1.
For example, the number 19 is a magic number because the sum of its digits (1 + 9) is 10, and the sum of 1 and 0 is 1.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing optimizations or additional features based on your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. 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 Magic Number), please comment here. I will help you immediately.