Skip to content

Commit 15bc6a5

Browse files
committed
new
1 parent 2607b37 commit 15bc6a5

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,99 @@
11
package com.sbiswas001.twelveproject;
22

3+
import java.util.Scanner;
4+
5+
/**
6+
* This class checks if a number is prime-palindrome
7+
* or not. A number is prime-palindrome if it is a
8+
* prime number as well as a palindrome number.
9+
* @author Sayan Biswas
10+
* @version 25.04.2022
11+
*/
312
public class PrimePalindrome {
13+
14+
/**
15+
* Stores number
16+
*/
17+
private int x;
18+
19+
/**
20+
* Initializes instance variables
21+
*/
22+
private PrimePalindrome() {
23+
x=0;
24+
}
25+
26+
/**
27+
* Inputs a number from user
28+
*/
29+
private void input() {
30+
Scanner sc = new Scanner(System.in);
31+
System.out.print("Enter a number: ");
32+
x=Integer.parseInt(sc.next());
33+
}
34+
35+
/**
36+
* Checks if a number is palindrome or not.
37+
* @param n Number to be checked
38+
* @return true or false
39+
*/
40+
private boolean palindromeCheck(int n)
41+
{
42+
return n == reverse(n);
43+
}
44+
45+
/**
46+
* Reverses a number
47+
* @param n Number to be reversed
48+
* @return Reversed number
49+
*/
50+
private int reverse(int n)
51+
{
52+
int sum=0, r;
53+
while(n>0)
54+
{
55+
r=n%10;
56+
sum=(sum*10)+r;
57+
n=n/10;
58+
}
59+
return sum;
60+
}
61+
62+
/**
63+
* Checks if number is prime or not
64+
* @param a Number to be checked
65+
* @return true or false
66+
*/
67+
private boolean primeCheck(int a) {
68+
int flag=0;
69+
if(a<2) return false;
70+
for(int i=2;i<a;i++) {
71+
if(a%i==0) {
72+
flag++;
73+
break;
74+
}
75+
}
76+
return flag != 1;
77+
}
78+
79+
/**
80+
* Displays if number is prime-palindrome or not.
81+
*/
82+
private void display() {
83+
System.out.println(palindromeCheck(x) && primeCheck(x) ?
84+
"Number is prime-palindrome." :
85+
"Number is not prime-palindrome.");
86+
}
87+
88+
/**
89+
* Calls other methods
90+
* @param args Arguments passed to main method
91+
*/
92+
public static void main(String[] args) {
93+
PrimePalindrome ob = new PrimePalindrome();
94+
ob.input();
95+
ob.display();
96+
}
97+
98+
499
}

0 commit comments

Comments
 (0)