Thread: Where is my Code Wrong

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

    Where is my Code Wrong

    so i need my program to display only palindromic prime numbers but its just displaying prime numbers and not displaying only palindroimc primes
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     #include <errno.h>
     #include <string.h>
     #include<math.h>
      FILE *outputFile;
      char buffer[81];
      int bytesWritten;
    
     main()
     {
      int isPrime(int);
    
      int i,j;
    
      long int n, num, rev = 0, dig;
    
       /*************************************/
     /*     Open file to write output     */
     /*************************************/
     outputFile = fopen("DD:OUTPUT", "w");
     if (outputFile == NULL)
     {
      printf("open error:   %d/%s\n", errno, strerror(errno));
      exit(99);
     }
    
     /*************************************/
     /*     Run program                   */
     /*************************************/
    
      for (i=1; i<15000; i++)
    
        {
        if (isPrime(i)==1)
        {
         n = num;
         while(num>0)
         {
              dig = num % 10;
              rev = rev * 10 + dig;
              num = num / 10;
         }
         if (n == rev)
    
    
         bytesWritten = sprintf(buffer,"%d is prime!!\n",i);
         fwrite(buffer, 1, bytesWritten, outputFile);
        }
      }
    
    
    
     /*************************************/
     /*     Close output file             */
     /*************************************/
     fclose(outputFile);
    
      return 0;
    }
    
    int isPrime (int myInt)
    {
    
    
      int loop;
    
      for (loop = 2; loop < myInt/2+1; loop++)
      {
        if (myInt%loop==0)
          return 0;
      }
        return 1;
     }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your offending code is
    Code:
         if (n == rev)
    
    
         bytesWritten = sprintf(buffer,"%d is prime!!\n",i);
         fwrite(buffer, 1, bytesWritten, outputFile);
    This will always produce output, regardless of whether the value is palindromic. An extra curly brace or two would fix your problem.

    It is rather pointless to open DD:OUTPUT and use fwrite() to produce output. Better to use standard text output to stdout. And it is advisable to declare main as returning int, rather than relying on implicit return type.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    an extra curly brace where?
    Quote Originally Posted by grumpy View Post
    Your offending code is
    Code:
         if (n == rev)
    
    
         bytesWritten = sprintf(buffer,"%d is prime!!\n",i);
         fwrite(buffer, 1, bytesWritten, outputFile);
    This will always produce output, regardless of whether the value is palindromic. An extra curly brace or two would fix your problem.

    It is rather pointless to open DD:OUTPUT and use fwrite() to produce output. Better to use standard text output to stdout. And it is advisable to declare main as returning int, rather than relying on implicit return type.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I have given you all the hints necessary. You can work out, for yourself, where curly braces are needed.

    After all, this is your homework.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with this code? please help..
    By thebridge in forum C Programming
    Replies: 2
    Last Post: 10-07-2010, 02:06 PM
  2. What is wrong with my code?
    By yann in forum C Programming
    Replies: 21
    Last Post: 10-10-2009, 07:51 AM
  3. what is wrong with this code?
    By timhxf in forum C Programming
    Replies: 5
    Last Post: 11-25-2006, 09:46 AM
  4. What's Wrong With My Code?
    By javacvb in forum C Programming
    Replies: 14
    Last Post: 06-15-2003, 12:09 PM
  5. What is wrong with my code?
    By kewee7197 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2002, 06:40 AM