PHP Basic
PHP Interview Programs
- PHP Interview Programs
- PHP Abundant Number
- PHP Amicable Number
- PHP Armstrong Number
- PHP Average of N Numbers
- PHP Automorphic Number
- PHP Biggest of three numbers
- PHP Binary to Decimal
- PHP Common Divisors
- PHP Composite Number
- PHP Condense a Number
- PHP Cube Number
- PHP Decimal to Binary
- PHP Decimal to Octal
- PHP Disarium Number
- PHP Even Number
- PHP Evil Number
- PHP Factorial of a Number
- PHP Fibonacci Series
- PHP GCD
- PHP Happy Number
- PHP Harshad Number
- PHP LCM
- PHP Leap Year
- PHP Magic Number
- PHP Matrix Addition
- PHP Matrix Division
- PHP Matrix Multiplication
- PHP Matrix Subtraction
- PHP Matrix Transpose
- PHP Maximum Value of an Array
- PHP Minimum Value of an Array
- PHP Multiplication Table
- PHP Natural Number
- PHP Number Combination
- PHP Odd Number
- PHP Palindrome Number
- PHP Pascalβs Triangle
- PHP Perfect Number
- PHP Perfect Square
- PHP Power of 2
- PHP Power of 3
- PHP Pronic Number
- PHP Prime Factor
- PHP Prime Number
- PHP Smith Number
- PHP Strong Number
- PHP Sum of Array
- PHP Sum of Digits
- PHP Swap Two Numbers
- PHP Triangular Number
PHP Program to Check Automorphic Number
Photo Credit to CodeToFun
π Introduction
In the realm of php 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 delve into a PHP 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 explore the PHP code that accomplishes this task.
<?php
// Function to check if a number is automorphic
function isAutomorphic($num)
{
// Calculate the square of the number
$square = $num * $num;
// Count the number of digits in the original number
$originalDigits = strlen((string)$num);
// Extract the last digits from the square
$lastDigits = $square % (int)pow(10, $originalDigits);
// Check if the last digits match the original number
return $lastDigits == $num;
}
// Driver program
// Replace this value with your desired number
$number = 76;
// Check if the number is automorphic
if (isAutomorphic($number))
{
echo "$number is an automorphic number.\n";
}
else
{
echo "$number is not an automorphic number.\n";
}
?>
π» 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 counts the number of digits in the original number to extract the corresponding number of last digits from the square.
- It compares the extracted last digits with the original number to determine if it's an automorphic number.
π Between the Given Range
Let's take a look at the PHP code that checks for Automorphic Numbers in the specified range.
<?php
// Function to check if a number is Automorphic
function isAutomorphic($number)
{
$square = $number * $number;
$numberDigits = strlen((string)$number);
// Extract the last digits of the square equal to the number's digits
$lastDigits = substr($square, -$numberDigits);
return ($lastDigits == $number) ? true : false;
}
// Check Automorphic Numbers in the range 1 to 50
echo "Automorphic Numbers in the range 1 to 50:\n";
for ($i = 1;$i <= 50;$i++)
{
if (isAutomorphic($i))
{
echo $i . " ";
}
}
?>
π» Testing the Program
Automorphic Numbers in the range 1 to 50: 1 5 6 25
Run the script to check for Automorphic Numbers in the specified 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 extracts the last digits equal to the number of digits in the original number.
- The main section checks and prints the Automorphic Numbers in the range from 1 to 50.
π§ 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 (PHP Program to Check Automorphic Number), please comment here. I will help you immediately.