Professional Documents
Culture Documents
Automorphic and Prime Number Menu Driven Java Program - KnowledgeBoat
Automorphic and Prime Number Menu Driven Java Program - KnowledgeBoat
Write a menu driven program to accept a number from the user and check
whether it is a Prime number or an Automorphic number.
(a) Prime number: (A number is said to be prime, if it is only divisible by 1 and
itself)
Example: 3,5,7,11
(b) Automorphic number: (Automorphic number is the number which is
contained in the last digit(s) of its square.)
Example: 25 is an Automorphic number as its square is 625 and 25 is present as
the last two digits.
Java Java Iterative Stmts ICSE
121 Likes
ANSWER
import java.util.Scanner;
https://www.knowledgeboat.com/question/write-a-menu-driven-program-to-accept-a-number-from-the-user--32577997686630360?studylist=num… 1/7
10/11/23, 2:27 PM Automorphic and Prime Number Menu Driven Java Program | KnowledgeBoat
switch (choice) {
case 1:
int c = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
c++;
}
}
if (c == 2)
System.out.println(num + " is Prime");
else
System.out.println(num + " is not Prime");
break;
case 2:
int numCopy = num;
int sq = num * num;
int d = 0;
/*
* Count the number of
* digits in num
*/
while(num > 0) {
d++;
num /= 10;
}
/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % Math.pow(10, d));
if (ld == numCopy)
Prev System.out.println(numCopy + " is automorphic");
All Questions Next
else
System.out.println(numCopy + " is not automorphic");
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
OUTPUT
BlueJ output of Write a menu driven program to accept a number from the user and check whether it is a Prime
number or an Automorphic number. (a) Prime number: (A number is said to be prime, if it is only divisible by 1 and itself)
Example: 3,5,7,11 (b) Automorphic number: (Automorphic number is the number which is contained in the last digit(s) of
its square.) Example: 25 is an Automorphic number as its square is 625 and 25 is present as the last two digits.
https://www.knowledgeboat.com/question/write-a-menu-driven-program-to-accept-a-number-from-the-user--32577997686630360?studylist=num… 2/7
10/11/23, 2:27 PM Automorphic and Prime Number Menu Driven Java Program | KnowledgeBoat
BlueJ output of Write a menu driven program to accept a number from the user and check whether it is a Prime
number or an Automorphic number. (a) Prime number: (A number is said to be prime, if it is only divisible by 1 and itself)
Example: 3,5,7,11 (b) Automorphic number: (Automorphic number is the number which is contained in the last digit(s) of
its square.) Example: 25 is an Automorphic number as its square is 625 and 25 is present as the last two digits.
Answered By
40 Likes
https://www.knowledgeboat.com/question/write-a-menu-driven-program-to-accept-a-number-from-the-user--32577997686630360?studylist=num… 4/7
10/11/23, 2:27 PM Automorphic and Prime Number Menu Driven Java Program | KnowledgeBoat
https://www.knowledgeboat.com/question/write-a-menu-driven-program-to-accept-a-number-from-the-user--32577997686630360?studylist=num… 5/7
10/11/23, 2:27 PM Automorphic and Prime Number Menu Driven Java Program | KnowledgeBoat
STUDYLIST
https://www.knowledgeboat.com/question/write-a-menu-driven-program-to-accept-a-number-from-the-user--32577997686630360?studylist=num… 6/7
10/11/23, 2:27 PM Automorphic and Prime Number Menu Driven Java Program | KnowledgeBoat
COMPANY
About Us
Contact Us
Privacy Policy
Terms of Service
https://www.knowledgeboat.com/question/write-a-menu-driven-program-to-accept-a-number-from-the-user--32577997686630360?studylist=num… 7/7