Java Find Automorphic numbers


 

Java Find Automorphic numbers

In this section, you will learn how to find the automorphic numbers between 1 and 1000.

In this section, you will learn how to find the automorphic numbers between 1 and 1000.

Java Find Automorphic numbers

In this section, you will learn how to find the automorphic numbers between 1 and 1000.

Automorphic numbers are the numbers whose square ends with the number itself or you can say it is a number whose square ends with the given integer. For example,5 is an automorphic number as the square of 5 is 25 and its square consists of number 5 in the end. Similarly 6 is an automorphic number as the square of 6 is 36 and its square consists of number 6 at the end. Here we are going to find the automorphic numbers between 1 and 1000.

Here is the code:

class FindAutomorphicNumber {
	public static void main(String args[]) {
		long num, n, sq, n1, n2;
		System.out.println("Automorphic Numbers between 1 and 1000 are: ");
		for (long i = 1; i < 1000; i++) {
			n = i;
			sq = n * n;
			while (n > 0) {
				n1 = n % 10;
				n2 = sq % 10;
				if (n1 != n2)
					break;
				n = n / 10;
				sq = sq / 10;
			}
			if (n == 0) {
				System.out.println(i);
			}
		}
	}
}

Output:

1
5
6
25
76
376
625

Ads