Results 1 to 8 of 8
  1. #1
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Palindrome Prime program

    Hey guys,

    I have to write a program using methods that displays all palindrome prime numbers when a user inputs a number, N, up to 9626. I really need some guidelines and advice because I honestly do not know where to start. Hope you can help, thanks.

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Palindrome Prime program

    Start with writing out your algorithm. Define your input, output,variables and data types that you will need.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Palindrome Prime program

    Try to break the problem down into more tractable pieces that, together, will give you what you want. For example write a method that checks whether a given number is a prime. And another to check whether a number is a palindrome.

    Come up with a *plan*, as this may guide how you break the problem into pieces. For instance, it strikes me that it might be more efficient to examine just palindromes for primeness rather than checking every number to see if it is a palindrome. So a method that can be used to create all the palindromic numbers might be useful (A method that takes a number and returns a palindrome would be useful here: 41 -> 141, etc. We needn't worry about the even length palindromes 41 -> 1441: why?)

  4. #4
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: Palindrome Prime program

    This might be a bit of a bump, but I don't want to create another thread. Thanks for the suggestions pbrockway2.

    I've written out my code for this program and I have a couple of errors that I can't seem to fix.

    Java Code:
    import java.util.*;
    public class Lab5
    {
       public static void main(String[] args)
       {
           int i=0;
           int j=0;
           int userInput;
           Scanner scn = new Scanner(System.in);
           System.out.println("Enter a positive number less than 9626");
           userInput=scn.nextInt();
    	if (userInput>9626) {
    		System.out.println("Number cannot be greater than 9626.");
    		System.exit(0);
    	}
    	   
    	for (i=1; i<userInput; i++) {
    		i=isPrime(i);
    		i=isPalindrome(i);
    	}
    	System.out.println(i);
    }
         
    
    //checks whether an int is prime or not.
    public static boolean isPrime(int n) {
        //check if n is a multiple of 2
        if (n%2==0) return false;
        //if not, then just check the odds
        for(int i=3;i*i<=n;i+=2) {
            if(n%i==0)
                return false;
        }
        return true;
    }
        
    public static boolean isPalindrome(int n) {
    	int dig;
            int rev=0;
            while (n>0) {
                dig=n%10;
                rev=rev*10+dig;
                n=n/10;
            }
            if (n==rev) {
                return false;
    	}
    	return true;
    	}
        }
    The errors are in the two methods that I've called. I need to print out numbers less than the user's input, so I'm sure it doesn't make sense right now. Could I get some help with this?

    Lab5.java:32: error: incompatible types
    i=isPrime(i);
    ^
    required: int
    found: boolean
    Lab5.java:33: error: incompatible types
    i=isPalindrome(i);
    ^
    required: int
    found: boolean

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Palindrome Prime program

    required: int
    found: boolean
    isPrime() and is Palindrome() both return a boolean value (true/false, yes/no) but you are assigning them (for some reason) to an int variable i.

    What boolean expressions are good for is putting inside if statements: "if(isPalindrome(i))". Or joining together with and or or.

    -----

    Do check that your methods work as advertised.
    Last edited by pbrockway2; 10-19-2011 at 11:03 AM.

  6. #6
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: Palindrome Prime program

    Quote Originally Posted by pbrockway2 View Post
    isPrime() and is Palindrome() both return a boolean value (true/false, yes/no) but you are assigning them (for some reason) to an int variable i.

    What boolean expressions are good for is putting inside if statements: "if(isPalindrome(i))". Or joining together with and or or.

    -----

    Do check that your methods work as advertised.
    Well I fixed the errors and it prints out prime numbers without any problems, but it seems like the equation for the palindrome is wrong.

  7. #7
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Palindrome Prime program

    Two problems with the palindrome method:

    Firstly, you did it backwards. You want it to return true if n and rev are equal, and false if they are not. So instead of:
    Java Code:
        if (n==rev) {
            return false;
        }
        return true;
    say:
    Java Code:
        if (n==rev) {
            return true;
        }
        return false;
    Better yet, since the expression "n==rev" is itself a boolean value, you can just return it without using an if statement, like so:
    Java Code:
    return n==rev;
    The second problem is that you lose the original value of n. By the time you get out of your while loop, n will always be zero. To fix this, back up the value of n at the start of the method:
    Java Code:
    int backup=n;
    Then, at the end, rather than comparing n to rev, compare backup to rev:
    Java Code:
    return backup==rev;

  8. #8
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: Palindrome Prime program

    Oh awesome, it works! Thanks DiamondSoul.

Similar Threads

  1. Palindrome program
    By trinity in forum New To Java
    Replies: 4
    Last Post: 04-16-2011, 04:22 AM
  2. Palindrome program help
    By Nel in forum New To Java
    Replies: 1
    Last Post: 03-03-2011, 04:49 AM
  3. StringTokenizer in a Palindrome program
    By jeremyk in forum New To Java
    Replies: 10
    Last Post: 02-13-2010, 07:35 PM
  4. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM
  5. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •