Dremendo Tag Line

Input a number and check if it is an automorphic number or not using a function in Java

Function - Question 10

In this question, we will see how to input a number and check if it is an automorphic number or not in Java programming using a function. To know more about function click on the function lesson.

Q10) Write a program in Java to input a number and check if it is an automorphic number or not using a function.

A number N is said to be automorphic, if its square ends in N for example 5 is automorphic, because 52=25, which ends in 5, 25 is automorphic, because 252=625, which ends in 25.

Program

import java.util.Scanner;

public class Q10
{
    public static int automorphic(int num)
    {
        int x,t,s;
        x=1;
        t=num;
        while(t>0)
        {
            x=x*10;
            t=t/10;
        }

        s=num*num;   // square of the number
        if(s%x==num)
        {
            return 1;
        }
        return 0;
    }

    public static void main(String args[])
    {
        int n;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a numbers ");
        n=sc.nextInt();

        if(automorphic(n)==1)
        {
            System.out.print("Automorphic Number");
        }
        else
        {
            System.out.print("Not Automorphic Number");
        }
    }
}

Output

Enter a number 25
Automorphic Number
video-poster