Write a program to display all the Automorphic Numbers from 1 to 2000
Answers
Answer:
Write a program to display all the Automorphic Numbers from 1 to 2000. (Example: 6, 25) [Since 6 2 = 36 and in 36 last digit = 6; similarly, 25 2 = 625 and in 625 last two digits = 25] [Hint: In the square form of the number if the last part is equal to the original number then the number is termed as Automorphic Number.]
Answer:
package LabPrograms;
import java.util.*;
public class automorphic {
public static void main(String[]args)
{
for(int i= 1; i<=2000; i++)
{
int square = 0;
if(i%10== 1 || i%10 == 5 || i%10== 6)
{
square = i*i;
String mainnum = String.valueOf(i);
String squarenum = String.valueOf(square);
int lenofsquarenum = squarenum.length()-1;
int j,flag=0;
for(j = mainnum.length()-1; j>=0; j-- )
{
if(mainnum.charAt(j) != squarenum.charAt(lenofsquarenum))
{
flag=0;
break;
}
else
{
flag=1;
lenofsquarenum--;
}
}
if(flag==1)
{
System.out.println(i);
}
}
}
}
}
Explanation: