February 24 2015

Automorphic number java program

Automorphic number java program

In mathematics an automorphic number is a number whose square ends in the same digits as the number itself.

For example,

52 = 2562 = 36 ,252=625 , 762 = 5776

The sequence of automorphic numbers begins 1, 5, 6, 25, 76, 376, 625, 9376…

Algorithm

  • Take the input from the user (number)
  • Calculate number of digits in the number(digits)
  • Calculate square of the number(numberSquare)
  • Calculate the reminder of number divided by the 10 to power of digits(lastDigits)
  • If number is equal to last digits of the number,It is automorphic
  • Else it is not automorphic

Example

number=25 digits=2 numberSquare=625

lastDigits=625%(102)=625%100=25

lastDigits is equal to number, It is automorphic number

Automorphic number java program

 


package com.answersz.programs;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class AutomorphicNumber {

public static void main(String[] args) throws NumberFormatException,IOException {

// System.out.println("Automorphic number in java program   ");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter number to check Automorphic or not : ");

int number = Integer.parseInt(br.readLine());

int digits = getDigits(number);

int numberSquare = number * number;

int lastDigits = (int) (numberSquare % (Math.pow(10, digits)));

if (number == lastDigits) {

System.out.println("It is a Automorphic Number");

} else {

System.out.println("It is not a Automorphic Number");

}

}

/*

* getDigits method returns total of digits in the number

*/

public static int getDigits(int number) {

int digits = 0;

while (number > 0) {

number = number / 10;

digits++;

}

return digits;

}

}

Output:

Enter number to check Automorphic or not :

25

It is a Automorphic Number

Enter number to check Automorphic or not :

76

It is a Automorphic Number

 Enter number to check Automorphic or not :

11

It is not a Automorphic Number

 


Copyright © 2014. All rights reserved.

Posted February 24, 2015 by answersz in category Bluej Java Programs for ICSE Class 10 and 9 Practice, Core Java

Leave a Comment

Your email address will not be published. Required fields are marked *

*