An Automorphic number is a number whose square ends with the same digits as the original number. E.g – 5, 6, 76 etc.

Automorphic numbers

Steps to Check Automorphic Number in C:

  1. Take a number as input (num).
  2. Count number of digits in the number (n)
  3. Find Square of the number (sqr).
  4. Extract last n digits from the sqr (last).
  5. If last == num then number is Automorphic else not.

Here is the implementation of the steps in C.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main()
{
    int num, sqr, temp, last;
    int n =0;
 
    printf("Enter a number \n");
    scanf("%d",&num);
 
    sqr = num*num;  //calculating square of num
    temp = num;
 
    //Counting number of digits
    while(temp>0){
        n++;
        temp = temp/10;
    }
 
    //Extracting last n digits
    int den = floor(pow(10,n));
    last = sqr % den;
 
    if(last == num)
        printf("Automorphic number \n");
    else
        printf("Not Automorphic \n");
 
    return 0;
}

Output

Automorphic Number in C

Find all Automorphic Numbers in the Interval

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
void automorphic(int num){
 
    int sqr, temp, last;
    int n =0;
 
    sqr = num*num;  //calculating square of num
    temp = num;
 
    //Counting number of digits
    while(temp>0){
        n++;
        temp = temp/10;
    }
 
    //Extracting last n digits
    int den = floor(pow(10,n));
    last = sqr % den;
 
    if(last == num)
        printf("%d \n",num);
}
 
int main()
{
    int upper, lower, i;
 
    printf("Enter lower interval \n");
    scanf("%d",&lower);
    printf("Enter upper interval \n");
    scanf("%d",&upper);
 
    printf("-------------------------- \n");
 
    for(i=lower; i<upper; i++){
        automorphic(i);
    }
    return 0;
}

Output

Automorphic numbers from 1 to 1000 in C

In this tutorial, we have learned to check automorphic in C.

Leave a Reply

thirteen − four =