Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Write a menu‐driven class to accept a number from the user and check whether it is a Automorphic number or not in Java.

Program to demonstrate a menu‐driven class to accept a number from the user and check whether it is a Automorphic number or not in Java.

package com.hubberspot.java.example;

import java.util.Scanner;

public class AutomorphicTest {

    public static void main(String[] args) {

        char program;
        Scanner scanner = new Scanner(System.in);
        do { 

            System.out.println("Press 'A' for Automorphic Number Test or 'E' for Exit");

            program = scanner.nextLine().toUpperCase().charAt(0);

            if(program != 'A' && program != 'E') { 
                System.out.println();
                System.out.println("You entered : " + program);
                System.out.println("Please enter P or E to proceed ... try again");
                System.out.println();
                continue;
            } 
            else { 
                switch( program )
                {
                case 'A': 
                    System.out.print( "Enter number for Automorphic Number checking: " );

                    int number = scanner.nextInt();
                    String line = scanner.nextLine();

                    if( isAutomorphicNumber( number ) )
                    {
                        System.out.println( number + " is a Automorphic number." );
                    }
                    else
                    {
                        System.out.println( number + " is not a Automorphic number." );
                    }
                    break;
                }
            }

            System.out.println();

            if( program == 'E') { 
                System.out.println("Exiting from the program...");
            }
        }
        while(program != 'E');


    }

    private static boolean isAutomorphicNumber(int number) {

        int square = number * number;

        String numberToString = "" + number;
        String squareToString = "" + square;
        
        if( squareToString.endsWith( numberToString ) )
        {
            return true;
        }
        return false;

    }

}



Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot