Thursday, 28 January 2016

Write a program in java to print all automorphic numbers with range.

Automorphic Number:
An automorphic number is a number whose square ends in the same digits as the number itself.
Examples of automorphic numbers : 5, 6 and 76
52 = 25
62 = 36
762 = 5776

Code:
import java.util.*;
class Automorphic_Range
{
  public static void main(String args[])
  {
    Scanner sc=new Scanner(System.in);
    int sqr,temp,c=0,i=1,rem;
    System.out.println("Enter The Range: ");
    int n=sc.nextInt();
    while(n!=0)
    {
    temp=i;
    sqr=i*i;
    while(i!=0)
    {
      c++;
      i=i/10;
    }
    rem=sqr % ((int)Math.pow(10,c));
    if(temp == rem)
    {
      System.out.println(i + ", ");
    }
   }
  }
}

No comments:

Post a Comment