-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime_Palindrome.java
More file actions
97 lines (93 loc) · 4.04 KB
/
Prime_Palindrome.java
File metadata and controls
97 lines (93 loc) · 4.04 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*;
public class Prime_Palindrome {
//Outer class 1
static class Palindrome{
//Nested class 1
class Checker{
//Method to check if number is Palindrome
public boolean isPalin(int num){
int original=num,rev=0;
while(num!=0){
int d=num%10;
rev=rev*10+d;
num/=10;
}
return (original==rev);
}
}
}
//Outer class 2
static class Prime{
//Nested class 2
class Checker{
//Method to check if number is Prime
public boolean isPrime(int n){
int i,count=0;
for(i=2;i<=Math.sqrt(n);i++){
if(n%i==0)
count=1;
}
if(count==1)
return false;
else
return true;
}
}
}
public static void main(String args[]){//main method
Scanner sc=new Scanner(System.in);
int choice;
boolean exit=false;
do{
System.out.println("---MENU---");
System.out.println("1. Palindrome Checker");
System.out.println("2. Prime Checker");
System.out.println("3. Prime_Palindrome Checker");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice=sc.nextInt();
System.out.println();
switch(choice){//Implementing switch-case
case 1: do{
System.out.print("Enter a number: ");
int num=sc.nextInt();
Palindrome.Checker ch=new Palindrome().new Checker();
if(ch.isPalin(num))
System.out.println(num + " is a Palindrome number.");
else
System.out.println(num + " is not a Palindrome number.");
System.out.println("Do you want to continue? (y/n): ");
}while(sc.next().charAt(0)=='y'); // while(choice.equalsIgnoreCase("y"));
break;
case 2: do{
System.out.print("Enter a number: ");
int num1=sc.nextInt();
Prime.Checker ch1=new Prime().new Checker();
if(ch1.isPrime(num1))
System.out.println(num1 + " is a Prime number.");
else
System.out.println(num1 + " is not a Prime number.");
System.out.println("Do you want to continue? (y/n): ");
}while(sc.next().charAt(0)=='y'); // while(choice.equalsIgnoreCase("y"));
break;
case 3: do{
System.out.print("Enter a number: ");
int n=sc.nextInt();
Palindrome.Checker a=new Palindrome().new Checker();
Prime.Checker b=new Prime().new Checker();
if(a.isPalin(n) && b.isPrime(n))
System.out.println(n + " is a Palindrome as well as Prime.");
else
System.out.println(n + " is neither a Palindrome nor a Prime.");
System.out.println("Do you want to continue? (y/n): ");
}while(sc.next().charAt(0)=='y');
break;
case 4: System.out.println("Exiting...Thank you for your responses!!");
exit=true;
break;
default:System.out.println("Invalid choice!!Press anything from 1-4");
}
}while(!exit);
sc.close();
}
}