## Q. Write an algorithm and program to check Automorphic Number?

Program to check Automorphic Number : This section focuses on Automorphic Number algorithm and program. This program should be practiced to improve the coding skills required for various interviews (campus interviews, walk-in interviews), coding rounds etc.

Automorphic Number : A number is called Automorphic number if and only if its square ends in the same digits as the number itself.
Here we are given a number n and our task to check the square of given number ends with original number or not.
For Example :-

Input : n = 76
Output : Yes, The number is Automorphic
Explanation : As we can see that input number is 76. The square of number 76 is 5776. So, output for the following code is "Yes, The number is Automorphic Number".
Now let us understand what logic we need to use in this:
1. Store the square of given number.
2. Using loop check if square ends in the same digits as the number itself.

## Algorithm To Check Automorphic Number

```\\Algorithm To Check Automorphic Number
START
Step 1 : Store the square of given number
Step 2 : Loop until N becomes 0
Check if (n%10 == sq%10)
if not equal, return false
Otherwise continue i.e. reduce number and square i.e. n = n/10 and sq = sq/10;
Step 3 : Return true if all digits matched
Stop
```

# Program To Check Automorphic Number

• C
• C++
• Java
• Python
• C#
• PHP
• ```//C Program To Check Automorphic Number
#include <stdio.h>

int main()
{
int number = 25, flag=1;
int sq = number * number;
while (number > 0) {
// Return 0, if any digit of number doesn't
// match with its square's digits from last
if (number % 10 != sq % 10)
flag=0;

// Reduce number and square
number /= 10;
sq /= 10;
}

if(flag==1)
{
printf("Yes, The number is Automorphic Number");
}
else
{
printf("No, The number is not Automorphic Number");
}

return 0;
}
```
```//C++ Program To Check Automorphic Number
#include <iostream>
using namespace std;

int main()
{
int number = 25, flag=1;
int sq = number * number;
while (number > 0) {
// Return 0, if any digit of number doesn't
// match with its square's digits from last
if (number % 10 != sq % 10)
flag=0;

// Reduce number and square
number /= 10;
sq /= 10;
}

if(flag==1)
{
cout<<"Yes, The number is Automorphic Number";
}
else
{
cout<<"No, The number is not Automorphic Number";
}

return 0;
}

```
```//Java Program To Check Automorphic Number

public class LFC
{
public static void main(String[] args) {

int number = 25, flag=1;
int sq = number * number;
while (number > 0) {
// Return 0, if any digit of number doesn't
// match with its square's digits from last
if (number % 10 != sq % 10)
flag=0;

// Reduce number and square
number /= 10;
sq /= 10;
}

if(flag==1)
{
System.out.println("Yes, The number is Automorphic Number");
}
else
{
System.out.println("No, The number is not Automorphic Number");
}
}
}

```
```#Python Program To Check Automorphic Number

number =25
sq=number*number #square of the given number
flag=0 #condition variable
while(number>0): #loop until number becomes 0
if(number%10!=sq%10):
print("No, The number is not Automorphic Number")
flag=1
# come out of the loop if the above condition holds true
break
#reduce number and square
number=number//10
sq=sq//10

if(flag==0):
print("Yes, The number is Automorphic Number")
```
```//C# Program To Check Automorphic Number

using System;
class LFC {
static void Main() {

int number = 25, flag=1;
int sq = number * number;
while (number > 0) {
// Return 0, if any digit of number doesn't
// match with its square's digits from last
if (number % 10 != sq % 10)
flag=0;

// Reduce number and square
number /= 10;
sq /= 10;
}

if(flag==1)
{
Console.WriteLine("Yes, The number is Automorphic Number");
}
else
{
Console.WriteLine("No, The number is not Automorphic Number");
}

}
}
```
```<?php
//PHP Program To Check Automorphic Number

function automorphic(\$number)
{
// Store the square
\$sqrt = \$number * \$number;

// Start Comparing digits
while (\$number > 0)
{
// Return false, if any
// digit of number doesn't
// match with its square's
// digits from last
if (\$number % 10 != \$sqrt % 10)
return -1;

// Reduce number and square
\$number /= 10;
\$sqrt /= 10;
}

return 1;
}

//Main Code
\$number = 25;

\$lfc = automorphic(\$number) ?
"Yes, The number is Automorphic Number" :
"No, The number is not Automorphic Number";
echo \$lfc;
```
```//C Program To Check Automorphic Number
#include <stdio.h>

int main()
{
int fact = 145;
int f;

// Fills factorials of digits from 0 to 9.
f = f = 1;
for (int i = 2; i<10; ++i)
f[i] = f[i-1] * i;

int sum = 0;
// Traverse through all digits of fact.
int temp = fact;
while (temp)
{
sum += f[temp%10];
temp /= 10;
}

//check if sum equal to orignal number
if(sum == fact)
{
printf("Yes, The number is Automorphic Number");
}
else
{
printf("No, The number is not Automorphic Number");
}

return 0;
}
```

#### Output

```Yes, The number is Automorphic Number
```

## Recommended Programs

Program to check Abundant Number
Program to Check Strong Number