|
| 1 | +importjava.util.Scanner; |
| 2 | +publicclassPalindromePrime { |
| 3 | + |
| 4 | +publicstaticvoidmain(String[]args) {// Main funtion |
| 5 | +Scannerin =newScanner(System.in); |
| 6 | +System.out.println("Enter the quantity of First Palindromic Primes you want"); |
| 7 | +intn =in.nextInt();// Input of how mant first pallindromic prime we want |
| 8 | +funtioning(n);// calling funtion - functioning |
| 9 | + } |
| 10 | + |
| 11 | +publicstaticbooleanprime(intnum) {// checking if number is prime or not |
| 12 | +for (intdivisor =2;divisor <=num /2;divisor++) { |
| 13 | +if (num %divisor ==0) { |
| 14 | +returnfalse;// false if not prime |
| 15 | + } |
| 16 | + } |
| 17 | +returntrue;// True if prime |
| 18 | + } |
| 19 | + |
| 20 | +publicstaticintreverse(intn){// Returns the reverse of the number |
| 21 | +intreverse =0; |
| 22 | +while(n!=0){ |
| 23 | +reverse =reverse *10; |
| 24 | +reverse =reverse +n%10; |
| 25 | +n =n/10; |
| 26 | + } |
| 27 | +returnreverse; |
| 28 | + } |
| 29 | + |
| 30 | +publicstaticvoidfuntioning(inty){ |
| 31 | +intcount =0; |
| 32 | +intnum =2; |
| 33 | +while(count <y){ |
| 34 | +if(prime(num) &&num ==reverse(num)){// number is prime and it's reverse is same |
| 35 | +count++;// counts check when to terminate while loop |
| 36 | +System.out.print(num +"\n");// Print the Palindromic Prime |
| 37 | + } |
| 38 | +num++;// inrease iterator value by one |
| 39 | + } |
| 40 | + } |
| 41 | +}; |