Question
Check whether a number is Adam number or not
(Adam number is a number whose square of the number and the square of the reversed number should be reverse to each other)
Enter the number:
13
SQUARE OF 13 IS :169
REVERSE OF 13 IS :31
SQUARE OF REVERSE OF NUMBER(31) IS :961
REVERSE OF 961 IS :169
SQUARE OF NUMBER IS EQUAL TO REVERSE OF SQUARE OF REVERSED NUMBER THEREFORE,
13 IS AN ADAM NUMBER
Enter the number:
19
SQUARE OF 19 IS :361
REVERSE OF 19 IS :91
SQUARE OF REVERSE OF NUMBER(91) IS :8281
REVERSE OF 8281 IS :1828
SQUARE OF NUMBER IS NOT EQUAL TO REVERSE OF SQUARE OF REVERSED NUMBER THEREFORE,
19 IS NOT AN ADAM NUMBER
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class AdamNumber
{
public static void main()
{
int num,squareOfNum=0,reverseOfNum=0,squareOfReverseOfNum=0,reverseOfSquaredReverseNumber=0;
//num variable will store the number which we will take from user
//squareOfNum variable will store the square of variable num
//reverseOfNum variable will store the reverse of the number
//squareOfReverseOfNum will store square of reverseOfNum variable
// and reverseOfSquaredReverseNumber will store the reverse of variable squareOfReverseOfNum
int temp=0,rem=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
num=sc.nextInt();
temp=num;
//Finding the reverse of the number
while(temp>0)
{
rem=temp%10;
reverseOfNum=reverseOfNum*10+rem;
temp=temp/10;
}
squareOfNum=num*num;
squareOfReverseOfNum=reverseOfNum*reverseOfNum;
temp=squareOfReverseOfNum;
//Finding the reverse of the square of the reversed number
while(temp>0)
{
rem=temp%10;
reverseOfSquaredReverseNumber=reverseOfSquaredReverseNumber*10+rem;
temp=temp/10;
}
if(reverseOfSquaredReverseNumber==squareOfNum)
{
//the first five lines in this if condition can be removed
//it is only for making it easier to understand
System.out.println("SQUARE OF "+num+" IS :"+squareOfNum);
System.out.println("REVERSE OF "+num+" IS :"+reverseOfNum);
System.out.println("SQUARE OF REVERSE OF NUMBER("+reverseOfNum+") IS :"+squareOfReverseOfNum);
System.out.println("REVERSE OF "+squareOfReverseOfNum+" IS :"+reverseOfSquaredReverseNumber);
System.out.println("SQUARE OF NUMBER IS EQUAL TO REVERSE OF SQUARE OF REVERSED NUMBER THEREFORE,");
System.out.println(num+" IS AN ADAM NUMBER");
}
else
{
//the first five lines in this else condition can be removed
//it is only for making it easier to understand
System.out.println("SQUARE OF "+num+" IS :"+squareOfNum);
System.out.println("REVERSE OF "+num+" IS :"+reverseOfNum);
System.out.println("SQUARE OF REVERSE OF NUMBER("+reverseOfNum+") IS :"+squareOfReverseOfNum);
System.out.println("REVERSE OF "+squareOfReverseOfNum+" IS :"+reverseOfSquaredReverseNumber);
System.out.println("SQUARE OF NUMBER IS NOT EQUAL TO REVERSE OF SQUARE OF REVERSED NUMBER THEREFORE,");
System.out.println(num+" IS NOT AN ADAM NUMBER");
}
}
}