Q. Write a program to input a number and check whether it is an Automorphic
number or not. An automorphic number is that which is present at the last
of its square. For example, 25 is an automorphic number as it is present in
its square i.e. 625.
Plz do it fast.
Class 9th ICSE
Answers
Answer:
software program is the correct answer
Answer:
Java Program for Automorphic Number
Home>Java Programs>Java Program for Automorphic Number
An Automorphic number is a number whose square ends with the same digits as the original number. E.g – 25, 76, 376 etc.
Automorphic numbers
Steps to Check Automorphic Number in Java
Take a number as input (num).
Square the number (sqr).
Count the number of digits of (num) using while loop (c).
Compare the last (c) digits of (sqr) with the (num).
If they are equal then the number is Automorphic else not.
Check Whether a number is an Automorphic Number in Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;
public class Automorphic {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int num = in.nextInt();
int c=0, sqr = num*num;
int temp =num; //copying num
//countint digits of num
while(temp>0){
c++;
temp=temp/10;
}
int lastSquareDigits = (int) (sqr%(Math.pow(10,c)));
if(num == lastSquareDigits)
System.out.println("Automorphic number");
else
System.out.println("Not an Automorphic number");
}
}
Output
1
2
3
4
5
6
7
Enter a number
376
Automorphic number
Enter a number
22
Not an Automorphic number
Java Program to Find all Automorphic Numbers in the Interval
We know how to check whether a number is Automorphic or not using the above java program. Now, to find all Automorphic numbers in the given range, we need to define a function which returns true or false depending on whether the passed number as a parameter is automorphic or not. We will check for all the values in the given interval by passing it to the function one by one using loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
public class Automorphicrange {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int l, h;
System.out.println("Input lower interval value");
l = in.nextInt();
System.out.println("Input upper interval value");
h = in.nextInt();
System.out.println("Automorphic numbers between "+l+" and "+h);
for(int i=l; i<=h; i++){
if(checkAutomorphic(i))
System.out.print(i+" ");
}
}
private static boolean checkAutomorphic(int num) {
int c=0, sqr = num*num;
int temp =num; //copying num
//countint digits of num
while(temp>0){
c++;
temp=temp/10;
}
int lastSquareDigits = (int) (sqr%(Math.pow(10,c)));
return num == lastSquareDigits;
}
}
Output
1
2
3
4
5
6
Input lower interval value
1
Input upper interval value
100
Automorphic numbers between 1 and 100
1 5 6 25 76
Any Doubts comment below.
THIS POST HAS 2 COMMENTS
Soumojeet Ro Y 5 JUL 2020 REPLY
Can u please explain the process I can’t understand
Adarsh Kumar 7 JUL 2020 REPLY
To check if a number is automorphic or not, we first need to count the number of digits of the input number. For ex: 76 has 2 digits.
To count the digits we divide the number by 10 and at the same time increment ‘c’ which was initially 0. After some iterations the number will reduce to 0 and cannot be divided by 10 (76/10=7 -> 7/10=0 ). At this point the value of c will be the number of digits in the input number.
Now we know the count (c) of digits of the input number, we will use it to extract last ‘c’ digits from the ‘square of the input number’.
To extract last ‘c’ digits we will use sqr%(Math.pow(10,c).
Suppose 76 is the input, its square is 5776. Using while loop we got c=2. To extract last 2 digits of 5776 we will divide it by 100 (math.pow(10,2)). The remainder will get us last two digits(5776%100=76).
Now compare the last two digits with the input value i.e 76 == 76
True so automorphic number.
Leave a Reply
Comment
Your comment here...
Enter your name or username to comment
Name (required)
Enter your email address to comment
Email (required)
Enter your website URL (optional)
Website
Save my name, email, and website in this browser for the next time I comment.
Search for:
Search
Sharing Is Caring
Advertisement
Related Posts
Java Program for Palindrome Number
Java Program for Palindrome String
Reverse a String in Java [3 Methods]
Java Program to Reverse a Number
Convert Java List to Array [2 Methods]
Java Hacks and Tricks for Prograramming
Java Program to Find GCD of n numbers (Array)
Java Program to Print all Permutations of a String
Java Program to Check If two Strings are Anagram of each other
Java Program to Sort Strings in Alphabetical order
Java Program for Pronic (or Rectangular) Number
Java Program to Sort ArrayList of Objects by Property
Java Program for Tower of Hanoi (Recursion)
Java Program to Convert Decimal to Binary
Java Program to Convert Binary to Decimal
Java Program To Convert Decimal to Octal
Java Program to Output next Largest Number using same Digits