Check Whether Or Not the Number is an Automorphic Number in Java
Given an integer input, the Objective is to check whether the square of the number ends with the same number or not. Therefore, we’ll write a code to Check Whether or Not the Number is an Automorphic Number in Java Language.
Example
Input : 5
Output : Yes, it's an Automorphic Number
Check Whether or Not the Number is an Automorphic Number in Java
Given an integer input, the objective is to check whether or not the Number is an automorphic number or not. To do so we’ll check whether the square if the number ends with the number itself of not. For a number to be Automorphic, it’s square has to end with the number itself. Let’s implement the above mentioned logic in Java Language.
Automorphic NumberA Number that when squared ends with the number itself is known as the Automorphic Number.
Let's try and understand the concept of Automorphic Number,
Example
Input : 5
Output : 25
Explanation : Number = 5
when squared you get 25
as 25 ends with 5
From the above example, we prove that the number 5 is an Automorphic Number.
public class Main
{
public static void main(String[] args) {
int n = 376, sq = n * n ;
if(isAutomorphic(n) == 1)
System.out.println("Num: "+ n + ", Square: " + sq + " - is Automorphic");
else
System.out.println("Num: "+ n + ", Square: " + sq + " - is not Automorphic");
}
static int isAutomorphic(int n){
int square = n * n;
while(n != 0)
{
// means not automorphic number
if(n % 10 != square % 10){
return 0;
}
// reduce down numbers
n /= 10;
square /= 10;
}
// if reaches here means automorphic number
return 1;
}
}
public class Main {
public static void main(String[] args) {
int x=5;
int y=x*x;
if(y%10==x%10)
System.out.println("automorphic");
else
System.out.println("not");
}
}
public class autoMorphicNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number : “);
int num = sc.nextInt();
int flag = num, rem, howManuNum = 0;
int sqrRoot = num * num;
while (flag != 0) {
rem = flag % 10;
howManuNum++;
flag /= 10;
}
int i = 1;
int flag1 = num;
String temp = “”;
while (i <= howManuNum) {
rem = flag1 % 10;
temp = String.valueOf(rem) + temp;
flag1 /= 10;
i++;
}
int temp2 = Integer.parseInt(temp);
if (temp2 == num) {
System.out.println("Yes, The above number is Automarphic");
}else {
System.out.println("No, The above number is Automarphic");
}
public class Main {
public static void main(String[] args) {
int x=5;
int y=x*x;
if(y%10==x%10)
System.out.println(“automorphic”);
else
System.out.println(“not”);
}
}
package prep1;
import java.util.Scanner;
public class autoMorphicNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number : “);
int num = sc.nextInt();
int flag = num, rem, howManuNum = 0;
int sqrRoot = num * num;
while (flag != 0) {
rem = flag % 10;
howManuNum++;
flag /= 10;
}
int i = 1;
int flag1 = num;
String temp = “”;
while (i <= howManuNum) {
rem = flag1 % 10;
temp = String.valueOf(rem) + temp;
flag1 /= 10;
i++;
}
int temp2 = Integer.parseInt(temp);
if (temp2 == num) {
System.out.println("Yes, The above number is Automarphic");
}else {
System.out.println("No, The above number is Automarphic");
}
}
}
Kindly refer link for technical query
Join this Discord Channel for technical query <3
public class Main {
public static void main(String[] args) {
int x=5;
int y=x*x;
if(y%10==x%10)
System.out.println(“automorphic”);
else
System.out.println(“not”);
}
}
public class Main {
public static void main(String[] args) {
int x=5;
int y=x*x;
System.out.println(y%10);
System.out.println(x/10);
if(y%10==x%10)
System.out.println(“automphric”);
else
System.out.println(“not”);
}
}
System.out.println((((n*n)%10)==n) ? “Automorphic number” : “Not an automorphic number”);
System.out.println((((n*n)%10)==(n%10)) ? “Automorphic number” : “Not an automorphic number”);
Updated for any number!
class HelloWorld {
public static void main(String[] args) {
int num=10;
int sqt=num*num;
int count=0;
int temp=num;
while(temp!=0)
{
temp=temp/10;
count++;
}
temp=sqt;
int rem=0,rev=0;
for(int i=0;i<count;i++)
{
rem=temp%10;
rev=rev*10+rem;
temp=temp/10;
}
if(rev==num)
System.out.println("A");
else
System.out.println("No");
}
}