0

Im new to java and I was wondering how i would print prime palindrome without using strings and only methods.

This is what I have so far. I want to print every prime palindrome number before 50. I did this with prime numbers only and it was working but when I added in palindrome, it did not work.

EDIT: i added in the int original = number like one of the answers says but my output is always 2,3,5,7,11 and nothing more.

EDIT2(1 more question): I changed the value up to 1000 and my output is 2 3 5 7 11 313 353 373 383 727 757 787 797 919 929. The output is correct but isn't 101, 131, 151, 181, 191 also prime palindrome numbers? Why are they not included in the output?

public class primePalindrome {
public static void main (String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number");
    int num = input.nextInt();
    System.out.println("The prime palindrome numbers are \n");
    printPP(num);
}

public static void printPP(int numberOfPP) {
    final int NUMBER_OF_PP_PER_LINE = 10;
    int count = 0;
    int number = 2;

    while (number < numberOfPP) {
        if(isPrime(number) && isPalindrome(number)) {
            count++;

            if (count % NUMBER_OF_PP_PER_LINE ==0) {
                System.out.printf("%-5s\n", number);
            }
            else
                System.out.printf("%-5s", number);
        }
        number++;
    }
}

public static boolean isPrime(int number) {
    for (int divisor = 2; divisor <= number / 2; divisor++) {
        if (number % divisor == 0) {
            return false;
        }
    }
    return true;
}

public static boolean isPalindrome(int number) {
    int reverse = 0;
    int n = number;
    for (int i = 0; i <= number; i++) {
        int remain = number%10;
        number = number/10;
        reverse = reverse*10+remain;
    }
    if (reverse == n) {
        return true;
    }
}
        return false;

}

}

eric
  • 1
  • 1
  • 2
  • HOW did it not work? Unexpected results (what results did you get?)? Compile errors (which ones)? Be more specific. – SJuan76 Jun 19 '11 at 23:29

9 Answers9

3

There's only one 2-digit prime palindrome: 11. Every other 2-digit number is divisible by 11. Your output is thus correct.

Your isPalindrome is quite close: 1) Move the equality check outside the loop 2) Use while-loop. Using "for" results in omitting palindrome patterns 1X1, 2XX2 etc. 3) Dont't forget to preserve the argument:

public static boolean isPalindrome(int number) {
    int original = number;
    int reverse = 0;
    while (number > 0) {
        int digit = number%10;
        number = number/10;
        reverse = reverse*10+remain;
    }
    return reverse == original;
}
Boycott Russia
  • 11,818
  • 5
  • 28
  • 29
  • Can't comment on the question, commenting here. Please edit your question to include the code you've actually run to get "2 3 5 7 11 313 ..." It's hard to tell why the program misbehaves without its source :) – Boycott Russia Jun 20 '11 at 00:28
1
/* Palindrome Program In JAVA
  Credit: Code Nirvana (www.codenirvana.in)
*/
import java.util.Scanner;
class Palindrome{ 
     public static void main(String args[]){ 
       System.out.print("Enter Number: ");
       Scanner read = new Scanner(System.in);
       int num = read.nextInt();
       int n = num;
       //reversing number
       int rev=0,rmd; 
       while(num > 0) 
       { 
         rmd = num % 10; 
         rev = rev * 10 + rmd; 
         num = num / 10; 
       } 
       if(rev == n) 
         System.out.println(n+" is a Palindrome Number!"); 
       else 
         System.out.println(n+" is not a Palindrome Number!"); 
     } 
}
1

Since most of the answers above doesn't work for both positive and negative numbers, following is one which works for negative numbers as well.

private static boolean isPalindrome(int n) {
    int orignal = n, reversed = 0;
    while (n != 0) {
        reversed = (reversed * 10) + (n % 10);
        n /= 10;
    }
    return reversed == orignal;
}
Hemant
  • 4,537
  • 8
  • 41
  • 43
1

You were close. At the end you compare number to reverse. Unfortunately, number has been modified. You need to compare number's original value to reverse. Here's my modified version:

public static boolean isPalindrome(int number) {
    int original = number;
    int reverse = 0;
    for (int i = 0; i <= number; i++) {
        int remain = number % 10;
        number = number / 10;
        reverse = reverse * 10 + remain;
    }
    return reverse == original;
}
hoipolloi
  • 7,984
  • 2
  • 27
  • 28
0

What if the given input is a huge number or a string?

I believe the below code should work for any input.

private boolean isPalindrome(String s) {
    int lo = 0, hi = s.length()-1;
    while(lo < hi) {
        if(s.charAt(lo) == s.charAt(hi)) {
           lo++; hi--;
        } else {
            return false;
        }
    }
    return true;
}
Deepak
  • 214
  • 1
  • 2
  • 9
0
import java.util.Scanner;

/*if given number is same with reverse number 
  then this number is called as Palindrome number. */

public class PalindromeNumber { 
public static void main(String args[])
{
    int input,store,output=0;
    Scanner in=new Scanner(System.in);
    System.out.println("Enter a number for check.");
    input=in.nextInt();
    store=input;
    while (input!=0)
    {   
    output=output*10;
    output=output+input%10;
    input=input/10;
    }
    System.out.println(output);
     if (output == store) 
    {
        System.out.println("This is a palindrome number.");
    }
    else
    {
        System.out.println("This is not a palindrome number.");
    } 

    in.close();
}

}

0
import java.util.*;
/*
@ Author 12CSE54
@ Date 29.10.14
*/
public class cpalindrome
{
public static void main(String ar[])throws Exception
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number\n");
int n=sc.nextInt();
int s=0,r;
while(n>0)
{
r=n%10;
s=(s*10)+r;
n/=10;
}
if(n==s)
System.out.println("palindrome\n");
else
System.out.println("not a palindrome");
}
}
T.B.Nanda
  • 1
  • 1
0
import java.util.*;
public class PalPrime
{
    public boolean prime(int n)
    {
         int c=0;
         for(int i=1;i<=n;i++)
         {
             if(n%i==0)
                 c++;
         }
         if(c==2)
             return true;
         else
             return false;
    }

    public boolean palindrome(int n)
    {
         int rev=0,temp=n;
         while(temp!=0)
         {
             rev=rev*10+(temp%10);
             temp=temp/10;
         }
         if(rev==n)
             return true;
         else
             return false;
    }

    public static void main(String args[])
    {
         Scanner ob=new Scanner(System.in);
         System.out.println("Enter number to be checked");
         int num=ob.nextInt();
         PalPrime obj=new PalPrime();
         if(obj.prime(num)==true && obj.palindrome(num)==true)
             System.out.println(num+" is a Prime Palindrome i.e. a PalPrime Number");
         else
             System.out.println(num+" is not a PalPrime Number");
    }
 }

TRY THIS!

Libin Varghese
  • 1,506
  • 13
  • 19
  • 1
    Welcome to StackOverflow. It's generally considered helpful to explain how your answer solves the original poster's problem. Also, please check your formatting, it's difficult to read because not all of the code is formatted as code. – betseyb Mar 24 '17 at 18:28
0

This is easy to Understand

public class StringDemo {

 public static void main(String args[]) {
  if(palindrome("1211")){
      System.out.println("Yes IT IS palindrome");
  }
  if(palindrome("121")){
      System.out.println("Yes IT IS palindrome");
  }
  }
  public static boolean palindrome(Object o){
   String s=(String)o;
   boolean result=true;
   int temp=s.length()-1;
   for(int c=0;c<temp;c++){
       if(s.charAt(c)==s.charAt(temp)){
           temp--;
           //System.out.println("Pallidrom start "+ s.charAt(c));
           //System.out.println("Pallidrom end "+ s.charAt(temp));
       }else{
           System.out.println("NOT palindrome");
           return false;
       }
    }
    return result;
 }
}

Result is

enter image description here

Muhaiminur Rahman
  • 3,066
  • 20
  • 27