# Automorphic Number

·1 min
Problem Solving
Author
Mayukh Datta

Automorphic number or circular number is a number whose square ends in the same digits as the number itself.

For example, \[5^2 = 2\boldsymbol{5}\\ 6^2 = 3\boldsymbol{6}\\ 76^2 = 57\boldsymbol{76}\\ 890625^2 = 793212\boldsymbol{890625}\\ 7109376^2 = 5054322\boldsymbol{7109376}\] So, \(5, 6, 76, 890625, 7109376\) are all automorphic numbers.

Below is the C program to print automorphic numbers from \(1\) to \(n\):

#include<stdio.h> #include<stdbool.h> #include<math.h>

bool isautomorphic(int n);

int main(void){ int i, n;

``````printf("Enter n: ");
scanf("%d", &n);
printf("Automorphic numbers from 1 to %d are\\n", n);
for(i=1;i<=n;i++){
if(isautomorphic(i))
printf("%d, ", i);
}
return 0;
``````

}

bool isautomorphic(int n){ int len, r; unsigned long long sqr;

``````sqr = (unsigned long long)n \* n;
len = floor(log10(n)) + 1;
r = (int)pow(10, len);

if(sqr%r == n){
return true;
}else{
return false;
}
``````

}

Executing the program:

\$ gcc -o p automorphic_no.c -lm \$ ./p Enter n: 9999999 Automorphic numbers from 1 to 9999999 are 1, 5, 6, 25, 76, 376, 625, 9376, 90625, 109376, 890625, 2890625, 7109376,