Thursday , 23 March 2017
Home » Class 10 » Java Program to check for Automorphic Number

Java Program to check for Automorphic Number

Question:

Write a Program in Java to input a number and check whether it is an Automorphic Number or not.

Note: An automorphic number is a number which is present in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last digits

Programming Code:

/**
* The class Automorphic inputs a number and checks whether it is an Automorphic Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class Automorphic
{
  public static void main(String args[]) throws Exception
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a Number : "); // Inputting the number
    int n = sc.nextInt();
    int sq = n*n; // Finding the square

    String num = Integer.toString(n); // Converting the number to String
    String square = Integer.toString(sq); // Converting the square to String

    if(square.endsWith(num)) // If the square ends with the number then it is Automorphic
        System.out.print(n+" is an Automorphic Number.");
    else
        System.out.print(n+" is not an Automorphic Number.");
    }
}

Working:

We have made use of the String function “endsWith()” to solve this program. This program could be done in other ways but, using Strings and its functions, we have tried to simplify it as much as we can.

The “endsWith()” function checks whether a String ends with a given String or a character or not.

In the above program we have accepted the number from the user and then found its square. We then converted both of them to String types. then using the endsWith() function we checked whether the square ends with the number or not (i.e. whether the square of the number has the number as its last digits or not). If the square ends with the number then the number is an Automorphic number otherwise not.

Alternate Method (Without using Strings):

/**
* The class Automorphic inputs a number and checks whether it is an Automorphic Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class Automorphic
{
  public static void main(String args[]) throws Exception
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a Number : "); // Inputting the number
    int n = sc.nextInt();
    int sq = n*n; // Finding the square
    int c = 0, copy = n;
    
    // While loop for counting the number of digits in the number
    while(copy > 0)
    {
        c++;
        copy = copy/10;
    }
    
    /* Finding the end digits of the square. 
     * If the number has 2 digits, then we need to find last 2 digits of square 
     * i.e. do a 'sq % 100' operation
     */ 
    int end = sq % (int)Math.pow(10,c);
    
    if(n == end) // If the square ends with the number then it is Automorphic
        System.out.print(n+" is an Automorphic Number.");
    else
        System.out.print(n+" is not an Automorphic Number.");
    }
}

Output:

Enter a Number : 25
25 is an Automorphic Number.

Enter a Number : 6
6 is an Automorphic Number.

Enter a Number : 9
9 is not an Automorphic Number.

Check Also

[Question 1] ISC 2017 Computer Practical Paper Solved – Box Packing

Solution of Program 1 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input number of boxes to be packed and display the break-up of the cartons used in descending order of capacity

7 comments

  1. Useful things

  2. You Know what… I just love you guys. Your ways of solving the problems are just incredible !!! THANK YOU SO SO MUCH :* :* :*

  3. this thing is very usefull 🙂 thank you

  4. This is a very helpful page keep it up. ?????

  5. the program was very helpful for me during exam

  6. Saptarshi chakraborty

    can we use endswith function in ISC

Leave a Reply to guideforschool Cancel reply

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