Automorphic number in Java


An Automorphic number is the number that is contained in the last digit(s) of its square.write a program in java to accept a number and
check whether it is an Automorphic or not.use the function name as digits(int n) that returns the last digit(s)present in the number.
e.g is an Automorphic number.the square of 25 is 625 and 25 is present as last two digits.

//To check an Automorphic number by using function.

import java.util.*;
class Morphic
{
int digits(int m)
{
int p,c=0,r;
double k=0;
p=m*m;
while(m!=0)
{
m=m/10;
c=c+1;
}
k=p%(Math.pow(10,c));
r=(int)k;
return(r);
}
public static void main(String aa[])
{
Scanner in=new Scanner(System.in);
int n,b;
Morphic ob=new Morphic();
System.out.println(“Enter your number”);
n=in.nextInt();
b=ob.digits(n);
if(n==b)
System.out.println(n+”is an Automorphic number”);
else
System.out.println(n+”is not an Automorphic number”);
}
}

output:

mj

Related Posts

Comments are closed.