Java Program to check for Automorphic Number

 


Introduction


This program helps to check that entered number is a Automorphic Number or not.

In mathematics an automorphic number (sometimes referred to as a circular number) is a number whose square “ends” in the same digits as the number itself.
 
For example:
52 = 25,
62 = 36,
762 = 5776,
3762 = 141376
 
are all automorphic numbers.
 

Flowchart


Please find sample flowchart diagram for this program as given below –
 
 
 

Program (C0de)

 
Please find the program below –
 

package com.kw.sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* This Program helps to check entered number is an Automorphic Number or not.
* Example: 25,76 are an Automorphic Number.
*
* @author dsahu1
*
*/
public class KWAutomorphicNumberExample {

/**
* Main Method to execute Program.
*
* @param args
* @throws NumberFormatException
* @throws IOException
*/
public static void main(String[] args) throws NumberFormatException,
IOException {

// Instantiate BufferedReader object
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a Number : ");

// Input number
int num = Integer.parseInt(br.readLine());

// Calculate Square
int sqNumber = num * num;
int numberOfDigit = 0;
int temp = num;
// Find total Digit in Square Number
while (temp > 0) {
numberOfDigit++;
temp = temp / 10;
}
// Calculate digit end with
int calculatedNumber = sqNumber % (int) Math.pow(10, numberOfDigit);

// Validate number with calculatedNumber to check Automorphic Number
if (calculatedNumber == num) {
System.out.println(num + " is an Automorphic Number.");
} else {
System.out.println(num + " is not an Automorphic Number.");
}
}

}


Output

Scenario1:
 
Enter a Number : 25
25 is an Automorphic Number.
 
———————————
Enter a Number : 76
76 is an Automorphic Number.
 
 
Scenario2:
 
Enter a Number : 45
45 is not an Automorphic Number.
 
 
Scenario3:
 
 Enter a Number : -36
-36 is not an Automorphic Number.
 
 

Video Explanation


This video helps to understand flowchart and program which written in the above steps. There are various ways to write a program and we are not covering all of them as this is just for practice. Please post your program in the comment box so other can utilize it.

Please click on below video to understand the problem with detailed explanation.

 

Leave a Reply

Your email address will not be published. Required fields are marked *