Automorphic Number in Java
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 Number
A 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 5From the above example, we prove that the number 5 is an Automorphic Number.
Automorphic Number in C Programming
Example:
5 =(5)2 = 25 6 = (6)2 = 36 25 = (25)2 = 625 76=(76)2 = 5776 376 = (376)2 = 141376 890625 = (890625)2 = 793212890625
These numbers are the automorphic numbers.
Java Code
Run
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; } }
Output
Num: 376, Square: 141376 - is Automorphic
Some other methods to solve the problem :
Method 1 :
Run
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"); } }
Output :
automorphic
Method 2 :
Run
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("Automorphic Number"); else System.out.println("No Automorphic Number"); } }
Output :
No Automorphic Number
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
package pripInsta;
public class AutomorphicNumber {
public static void Automorphic(int num)
{
int rem,square=num*num;
if(square!=0)
{
rem=square%10;
if(rem!=num)
{
System.out.println(“not Automorphic Number”);
}
else
{
System.out.println(“is Automorphic Number”);
}
}
}
public static void main(String[] args) {
int num=4;
Automorphic(num);
}
}
Hey there,
Kindly join our Discord server for all the technical and subject related queries.
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");
}
}