Check whether a number is Automorphic number or not | faceprep

Program to check whether a number is Automorphic number or not is discussed here. An automorphic number is a number whose square ends with the number itself.

For example, 5 is an automorphic number, 5*5 =25. The last digit is 5 which same as the given number. It has only a positive single digit number. If the number is not valid, it should display “Invalid input“.

If it is an automorphic number display “Automorphic Number” else display “Not Automorphic Number”.

Input & Output format:

Input consist of an integer.

Sample Input :

7

Sample Output :

Not Automorphic Number

Flow chart to check whether a number is automorphic number or not

automorphic number or not

Program to check whether the number is automorphic or not is given below

// C program to check whether the number is automorphic or not

#include<stdio.h>

bool isAutomorphic(int N)
{
int sq = N * N;

while (N > 0)
{
if (N % 10 != sq % 10)
return false;

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

return true;
}

int main()
{
//Fill the code
int N;
scanf(“%d”,&N);
isAutomorphic(N) ? printf(“Automorphic”) : printf(“Not Automorphic”);
return 0;
}

// C++ program to check whether the number is automorphic or not

#include<iostream>
using namespace std;

bool isAutomorphic(int N)
{
int sq = N * N;

while (N > 0)
{
if (N % 10 != sq % 10)
return false;

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

return true;
}

int main()
{
//Fill the code
int N;
cin >> N;
isAutomorphic(N) ? cout << “Automorphic” : cout << “Not Automorphic”;
return 0;
}

// Java program to check whether the number is automorphic or not

import java.util.*;
public class Main
{
static boolean isAutomorphic(int N)
{
int sq = N * N;

while (N > 0)
{
if (N % 10 != sq % 10)
return false;

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

return true;
}
public static void main(String[] args)
{
int num;
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
if(isAutomorphic(num))
{
System.out.println(“Automorphic” );
}
else
System.out.println(“Not Automorphic”);

}
}

# Python program to check whether the number is automorphic or not

def isAutomorphic(N) :

# Store the square
sq = N * N

# Comparing digits
while (N > 0) :
if (N % 10 != sq % 10) :
return False

# Reduce N and square
N = int(N/10)
sq = int(sq/10)

return True

N = int(input())
if isAutomorphic(N) :
print (“Automorphic”)
else :
print (“Not Automorphic”)

Output:

automorphic number or not