-
Notifications
You must be signed in to change notification settings - Fork 82
/
PalPrime number
60 lines (49 loc) · 1.73 KB
/
PalPrime number
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
//import required classes and packages
import java.io.*;
import java.util.*;
//create PalPrimeNumber class to check whether the given number is a PalPrime number or not
class PalPrimeNumber
{
//main() method start
public static void main(String args[])
{
//create scanner class object to get input from user
Scanner sc = new Scanner(System.in);
//declare and initialize variables
int number, temp, rem, i;
int count = 0;
int sum = 0;
System.out.println("Enter number to check");
//get input from user
number = sc.nextInt();
//store number in a temporary variable temp
temp = number;
//use for loop check whether number is prime or not
for(i = 1; i <= temp; i++)
{
if(temp%i == 0)
{
count++; //increment counter when the reminder is 0
}
}
//use while loop to check whether the number is palindrome or not
while(number > 0)
{
// get last digit of the number
rem = number%10;
// store the digit last digit
sum = sum*10+rem;
// extract all digit except the last
number = number/10;
}
//check whether the number is palindrome and Prime or not
if(temp == sum && count == 2)
{
System.out.println(temp +" is a PalPrime number");
}
else
{
System.out.println(temp +" is not a PalPrime number");
}
}
}