. Write a program to display all the Automorphic Numbers from 1 to 2000. (Example: 6, 25) [Since 6 2 = 36 and in 36 last digit = 6; similarly, 25 2 = 625 and in 625 last two digits = 25] [Hint: In the square form of the number if the last part is equal to the original number then the number is termed as Automorphic Number.]
Answers
The required program is given below.
Explanation:
// C program to check print Authomorphic number from 1 to 2000
#include <stdio.h>
int Automorphic(int i)
{
int number,square;
number = i;
square = i*i;
while (number > 0)
{
// If any digit of number doesn't match with its square's digits
from last, it returns false (0).
if (number % 10 != square % 10)
return 0;
// Reduce number and square
number = number / 10;
square = square / 10;
}
return 1;
}
int main()
{
int i,check;
printf("Automorphic Numbers from 1 to 2000 are: \n");
for(i=1;i<=2000;i++)
{
check=Automorphic(i);
if(check==1)
printf("%d\t",i);
}
return 0;
}
Refer the attached image for the output.