Automorphic number or not using Java
Automorphic number or not using Java :
Automorphic number is a number whose square ends in the same digits as the number itself.
For better understanding lets consider an example :
- Suppose a number 5.
- Square of 5 is 25.
- and we see that 25 ends in the same digit as the number itself, means 5.
Hence we can conclude that 5 is an Automorphic Number.
Some more Automorphic Numbers are : 5 , 6 , 76 , 376 and 890625.
Step 1 : Ask the user to enter a number and store it in an integer variable.
Step 2 : Convert the integer value to String to perform string operations.
Step 3 : Calculate length of the number stored in String variable.
Step 4 : Calculate square of the number and store its value in an another integer variable.
Step 5 : Convert the square value in String variable and then calculate its length.
Step 6 : Make a substring from l2 – l1, where l2 is the length of square value and l1 is the length of the number entered by the user and store it in a variable.
Step 7 : Compare that whether the original number is equal to the substring value or not.
- If this gets true , then the given number is an Automorphic Number.
- Else the number is not an Automorphic Number.
Code in Java :
//Java program to check whether a number is Automorphic number or not
public class automorphic_number_or_not
public static void main(String args)
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a number : ");
int number = sc.nextInt();
//Convert the number to string
String s1 = Integer.toString(number);
//Calculate the length
int l1 = s1.length();
int sq = number * number;
String s2 = Integer.toString(sq);
int l2 = s2.length();
String s3 = s2.substring(l2-l1);
System.out.println("Not an Automorphic Number");
//closing scanner class(not compulsory, but good practice)
Enter a number : 5
Enter a number : 34
Not an Automorphic Number