Finding a number that is both a prime number and a palindrome

I' fairly import java.util.*; public class Lab5 { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a value:"); //Asking the user to input the value n int n =scanner.nextInt(); for (int y=2; y<=n; y++) { //For every number from 2 to n prime(y); //the prime number is checked pal(y); //and so is the palindrome if ((prime(y)==true) && (pal(y)==true)) { //if a number is both a prime AND a palindrome (both methods are being compared here) System.out.println(y); //it is printed out } } } public static boolean prime(int n) { //the method for finding the prime number int x = 2; while (n%x>0) { x+=1; } if (x==n) { return true; } else { return false; } } public static boolean pal(int n) { //the method for finding a palindrome int rev = 0; int rmd = 0; while (n>0) { rmd = n%10; rev = rev*10 + rmd; n = n/10; } if (rev==n) { return true; } else { return false; } } ew to java. I have code in which a use inputs a number and the program check every number from 1 to n and outputs every number that is both a prime number and a palindrome. However my code doesn't output anything for some reason. There are no errors in the code, so I'm not sure what's wrong exactly. Here's my code:

n

}

integer,compare,output,palindrome,

0

Ответов: 2


0

Your palindrome method is returning wrong result. You are modifying n and after then comparing with reversed number.

First save that value then compare after while loop

pal()

0

You are mistaking in your temp function. Your n variable is getting 0 and you are comparing it to reverse no.

Assign that variable to a temp variable and then compare. I made some changes to your code and it working great.

import java.util.*;
public class A 
{
    public static void main (String[] args)  {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a value:");  //Asking the user to input     the value n
        int n =scanner.nextInt();
        for(int i=2;i<=n;i++){
            if (prime(i) && pal(i)) { //if a number is both a prime AND a palindrome (both methods are being compared here)
                System.out.println(i);                          //it is printed out
            }
        }
    }
    public static boolean prime(int n) { //the method for finding the prime number
        int x = 2;
        while (n%x>0) {
            x+=1;
        } if (x==n) {
            return true;
        } else {
            return false;
        }
    }
    public static boolean pal(int n) { //the method for finding a palindrome
        int rev = 0;
            int rmd = 0;
        int temp = n;
            while (n>0) {
                rmd = n%10;
                rev = rev*10 + rmd;
                n = n/10;
            }
        if (rev==temp){
                return true;
            }else{
                return false;
            }
    }
}

Output :

enter image description here

integer,compare,output,palindrome,
Похожие вопросы
Яндекс.Метрика