Home Tutorial Java Core Java Find Automorphic numbers

 
 

Java Find Automorphic numbers
Posted on: January 15, 2011 at 12:00 AM
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

Related Tags for Java Find Automorphic numbers:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.