eric
eric

Reputation: 1

palindrome numbers in java

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;

}

}

Upvotes: 0

Views: 10156

Answers (9)

Muhaiminur Rahman
Muhaiminur Rahman

Reputation: 3150

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

Upvotes: 0

Subhashis
Subhashis

Reputation: 1

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!

Upvotes: 0

T.B.Nanda
T.B.Nanda

Reputation: 1

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");
}
}

Upvotes: 0

Nusrat Ahmed Asha
Nusrat Ahmed Asha

Reputation: 1

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();
}

}

Upvotes: 0

Deepak
Deepak

Reputation: 224

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;
}

Upvotes: 0

Hemant
Hemant

Reputation: 4616

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;
}

Upvotes: 1

Simran Sharma
Simran Sharma

Reputation: 19

/* 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!"); 
     } 
}

Upvotes: 1

Boycott Russia
Boycott Russia

Reputation: 12127

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;
}

Upvotes: 3

hoipolloi
hoipolloi

Reputation: 8044

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;
}

Upvotes: 1

Related Questions