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 Automorphic Number
Photo Credit to CodeToFun
π Introduction
In the realm of javascript programming, exploring unique number properties is both fascinating and educational. An automorphic number is one such interesting concept.
An automorphic number (or circular number) is a number whose square ends with the number itself. In simpler terms, an n-digit number is called an automorphic number if the last n digits of its square are equal to the number itself.
In this tutorial, we'll explore a JavaScript program that checks whether a given number is an automorphic number or not.
The program will take a number as input, square it, and then compare the last digits to determine if it meets the criteria of an automorphic number.
π Example
Let's delve into the JavaScript code that accomplishes this task.
// Function to check if a number is automorphic
function isAutomorphic(num) {
// Calculate the square of the number
const square = num * num;
// Convert the number and square to strings
const numStr = num.toString();
const squareStr = square.toString();
// Extract the last digits from the square
const lastDigits = squareStr.slice(-numStr.length);
// Check if the last digits match the original number
return parseInt(lastDigits) === num;
}
// Driver program
// Replace this value with your desired number
const number = 76;
// Check if the number is automorphic
if (isAutomorphic(number)) {
console.log(`${number} is an automorphic number.`);
} else {
console.log(`${number} is not an automorphic number.`);
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the code.
76 is an automorphic number.
Run the script to check if the given number is an automorphic number.
π§ How the Program Works
- The program defines a function isAutomorphic that takes a number as input and returns whether it is an automorphic number or not.
- Inside the function, it calculates the square of the input number.
- It converts the number and square to strings to easily extract the last digits.
- It extracts the corresponding number of last digits from the square.
- It checks if the extracted last digits match the original number to determine if it's an automorphic number.
π Between the Given Range
Let's explore the JavaScript code that checks for automorphic numbers in the specified range.
// Function to check if a number is automorphic
function isAutomorphic(num) {
const square = num * num;
const strNum = num.toString();
const strSquare = square.toString();
return strSquare.endsWith(strNum);
}
console.log("Automorphic Numbers in the range 1 to 50:");
// Check for automorphic numbers in the range 1 to 50
for (let i = 1; i <= 50; i++) {
if (isAutomorphic(i)) {
console.log(`${i} `);
}
}
π» Testing the Program
Automorphic Numbers in the range 1 to 50: 1 5 6 25
The program is set to check for automorphic numbers in the range 1 to 50. Run the script to see the automorphic numbers within this range.
π§ How the Program Works
- The program defines a function isAutomorphic that takes a number as input and checks if it is an automorphic number.
- Inside the function, it calculates the square of the number and converts both the original number and its square to strings.
- It checks if the square's string representation ends with the original number's string representation.
- The program then iterates through numbers in the range 1 to 50 and prints the automorphic numbers.
π§ Understanding the Concept of Automorphic Numbers
Before delving into the code, let's take a moment to understand automorphic numbers.
An n-digit number is called an automorphic number if the last n digits of its square are equal to the number itself.
For example, 25 is an automorphic number because its square is 625, and the last two digits (25) match the original number.
π’ Optimizing the Program
While the provided program effectively checks for automorphic numbers, you can explore and implement further optimizations or variations based on your specific needs or preferences.
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 Automorphic Number), please comment here. I will help you immediately.