write a program in Java to display all the four digit palindromic prime numbers
Answers
Answered by
20
Here's the code for the program. The description is given as comments. Copy the code into a suitable IDE, so that comments and code will be visible properly.
public class Palindrome_Number //Creating Class
{
public static void main(String[] args) //Creating main() function
{
int p,r,d,n; //Some int variables
System.out.println("The four digit palindrome numbers are: \n");
for(n=1000;n<=9999;n++) //This loop considers all four digit numbers, as n = 1000 to 9999
{
p=n; //Storing value of n into p. All calculations will be performed on p.
r=0; //r will contain the reverse of n
do
{
d=p%10; //Extracting digit of p onto d
r=r*10 + d; //Placing the digit d into appropriate place in r
p=p/10;
}
while (p!=0);
if (r==n) //Checking if reverse of n is equal to n
System.out.println(n); //Printing palindrome number
}
}
}
public class Palindrome_Number //Creating Class
{
public static void main(String[] args) //Creating main() function
{
int p,r,d,n; //Some int variables
System.out.println("The four digit palindrome numbers are: \n");
for(n=1000;n<=9999;n++) //This loop considers all four digit numbers, as n = 1000 to 9999
{
p=n; //Storing value of n into p. All calculations will be performed on p.
r=0; //r will contain the reverse of n
do
{
d=p%10; //Extracting digit of p onto d
r=r*10 + d; //Placing the digit d into appropriate place in r
p=p/10;
}
while (p!=0);
if (r==n) //Checking if reverse of n is equal to n
System.out.println(n); //Printing palindrome number
}
}
}
Similar questions