Thread: Prime palindrome

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Question Prime palindrome

    Here is my situation. I have seen different snippets of C code but have never really learned to program in it. I have a piece of code that I need to analyze, then modify to produce a certain output. I am hoping that someone here can point me in the direction I need to go... I am totally lost with this. Here is the source :

    Code:
    01 #include <stdio.h> 
    02 #include <stdlib.h> 
    03 #include <errno.h> 
    04 #include <string.h> 
    05 
    06 FILE *outputFile; 
    07 char buffer[81]; 
    08 int bytesWritten; 
    09 
    10 main() 
    11 { 
    12 int isPrime(int); 
    13 
    14 int i,j; 
    15 
    16 
    17 /*************************************/ 
    18 /* Open file to write output */ 
    19 /*************************************/ 
    20 outputFile = fopen("DD:OUTPUT", "w"); 
    21 if (outputFile == NULL) 
    22 { 
    23 printf("open error: %d/%s\n", errno, strerror(errno)); 
    24 exit(99); 
    25 } 
    26 
    27 /*************************************/ 
    28 /* Run program */ 
    29 /*************************************/ 
    30 
    31 for (i=1; i<15000; i++) 
    32 { 
    33 if (isPrime(i)==1) 
    34 { 
    35 bytesWritten = sprintf(buffer,"%d is prime!\n",i); 
    36 fwrite(buffer, 1, bytesWritten, outputFile); 
    37 } 
    38 } 
    39 
    40 /*************************************/ 
    41 /* Close output file */ 
    42 /*************************************/ 
    43 fclose(outputFile); 
    44 
    45 return 0; 
    46 } 
    47 
    48 int isPrime (int myInt) 
    49 { 
    50 
    51 int loop; 
    52 
    53 for (loop = 2; loop < myInt/2+1; loop++) 
    54 { 
    55 if (myInt%loop==0) 
    56 return 0; 
    57 } 
    58 return 1; 
    59 } 
    60
    Now, here is the out put that is necessary:

    Bob's Palindromic Primes
    (insert a blank line between header and first line of print)
    Prime number 2 is also a Palindrome
    Prime number 3 is also a Palindrome
    Prime number 5 is also a Palindrome
    Prime number 7 is also a Palindrome
    Prime number 11 is also a Palindrome
    Prime number 101 is also a Palindrome (etc. keeps repeating with primes that are palindromes)

    Here is some information that may be of help to you: The range of numbers to find primes palindromes is to be hard coded, not prompted for input from the user. Also, a JCL script is ran against this source member to generate the output to another member. I appreciate your time, any little bit may help. Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Just like the output file, open an input file that has the range of numbers hard coded, one per line.
    Then all that's needed is to read in each number and process it to see if it's a "palindromic prime".

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The direction you need to go starts with posting properly indented code without line numbers copied from your editor. Then you need to read this, fix your definition of main and add the requisite return statement at the end.

    Next, you need to think about the problem, and try solving it by hand. How would you do this with a paper and pencil (hint, you'll need to use the modulus/remainder operator)? Once you have the manual process down, you need to try and code it up. If you are still having trouble at that stage, post back and we'll see what we can do.

    If you don't actually know how to program, then you need to read some tutorials. We have some here, and Google has more. Get a good book and work through the examples.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    This look a bit to much like something you would be given as an assignment in school. If you spend a few minutes to think about it and know how to program C I cannot see that this is that much of a problem. What you are asking for is basically to find primes and then to read them backwards, this isn't as hard as you may think if you read the post above this. I do think you have been given more help then you deserve in this thread, so you need to solve the problem on yourself now.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Well, I do know a little about programming, just none in C. I have been reading up on some tutorials. The code I have posted already generates the prime numbers. The mod operator is being used in the myInt%loop == 0 return 0 code. I am just not sure what direction needs to be taken to get the code to then figure out what of those numbers are palindromes. If they are determined to be palindromes, they should issue return code 1. Like I said, I have been reading up on some tutorials but havent really found what I need... yet anyways. I have found some coding samples but they are all based upon a user inputted range. I am continuing to search and read tutorials, I was just hoping for a little help from the community, thats all. Thanks.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Or maybe I should figure out how to code the palindrome number section. Then of those that are palindromes, check to see if they are prime and return 1? Does this sound like a better approach?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have two separate problems. First, write a function to generate a list of primes, printed to the screen. Next, generate a function that will take a number and check to see if it is a palindrome. Finally, combine the two functions into one. Don't try to get them both working at the same time, work each half of the problem independently. Once you know each individual function works on its own, then think about doing the rest.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    I cannot really see any difference in the implementation between C or any other language for this problem. You are not using anything that is that advanced and what you should to to get this to work is to read anduril462's post again. The problem of identifying palindrome-numbers isn't that much different from finding the primes, you only need to think about what you are doing and then write the code. If you can do this in a "BASIC language" you can do it in C without any need to read up that much on what to do.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The "higher-level" logic for prime palindromes is easy:
    Code:
    if a number is a prime and it is a palindrome
        print it out
    You have the logic down for checking for prime numbers. You need to work out the palindrome number thing. A palindrome is the same forwards and backwards. So you need a copy of the number with the digits forwards and a copy with them backwards. If they're the same, your number is a palindrome. Now work out how to reverse the digits in a number. Not only do you need to use the modulus operator, it's important to know that you're probably trying to do this in base 10.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Cool

    Alright. I got it to work. I used a "while loop" after the prime "for loop". It determined if the number is palindromic or not by using the mod and division characters as well as a couple new variables. It was then followed up by a if, else statement that evaluated two variables and returned respective codes. All is working well.

    Now to tackle the header and blank line that I need to place at the top of the file. I have tried to change the w attribute to a attribute on line 20, then add an else statement
    Code:
    printf("Bob's Palindromin Primes!\n\n");
    I recompile it and I am just getting the numbers in the loop. I am not seeing this header and blank line showing at the top of the output. Am I locating this in the wrong spot? I wouldnt think it would go in the "for loop" on line 31, would it - or would that cause it to repeat? Thanks again.
    Last edited by brandname2007; 10-04-2011 at 08:51 PM.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Cool

    Ok. By toying around with different I/O functions I found out what I was doing wrong. Thanks to everyone for the pointers and even the "constructive criticism." I can definitely say I have learned quite a few things about plain ole C today.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-19-2009, 10:32 PM
  2. the palindrome
    By haochao in forum C Programming
    Replies: 15
    Last Post: 10-11-2008, 11:29 PM
  3. palindrome
    By chodmama in forum C Programming
    Replies: 1
    Last Post: 02-20-2006, 02:52 AM
  4. Replies: 3
    Last Post: 03-29-2005, 04:24 PM