C Program to Check Automorphic (Cyclic) Number (User Defined)

A number is called Automorphic or Cyclic number if and only if its square ends in the same digits as the number itself.

For Examples: 52 = 25, 62 = 36, 762 = 5776, 3762 = 141376

In this program automorphic() function takes given number as an argument and returns 1 (TRUE) if given number is AUTOMORPHIC otherwise it returns 0 (FALSE) if it is NOT AUTOMORPHIC

Program


#include<stdio.h>
#include<conio.h>

/* Function prototype */
int automorphic(long int number);

/* Main Function */
int main()
{
 long int number;
 clrscr();
 printf("Enter integer number:\n");
 scanf("%ld", &number);

 if(automorphic(number))
 {
  printf("AUTOMORPHIC");
 }
 else
 {
  printf("NOT AUTOMORPHIC");
 }
 getch();
 return 0;
}
/* Function Definition */
int automorphic(long int number)
{
 int square, flag=1;
 square = number * number;
 while(number!=0)
 {
  if(square%10 != number%10)
  {
   flag=0;
   break;
  }
  number = number/10;
  square = square/10;
 }
 return flag;
}

Output

Run 1:
-----------------
Enter integer number:
5 ↲
AUTOMORPHIC

Run 2:
-----------------
Enter integer number:
76 ↲
AUTOMORPHIC

Run 3:
-----------------
Enter integer number:
7 ↲
NOT AUTOMORPHIC


Note: ↲ indicates ENTER is pressed.