Thread: Prime Number Function

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Prime Number Function

    Hey guys. I'm new to the forums and C, but I think I'm learning quickly. I think I got the basic program down but I need to fine tune it for this next step.

    For my main function I need to call isprime from 1 to 1000 using a for loop. If the call isprime(n) returns a 1, I need to print that number. After I've done this, I need to seperate them into columns after every tenth number. This I can't figure out.

    Here's my code so far. Any hints would be appreciated. Thanks!

    Code:
    #include "stdafx.h"
    
    int isprime(int n);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int x, col = 0;
    
    	for ( x = 1; x <= 1001; x++)
    	{
    		if( isprime(x) == 1)
    			printf("%d  ", x);
    		
    	}
    
    	
    }
    
    int isprime(int n)
    {
    	int div;
    
    	if(n == 2)
    		return 1;
    
    	for(div = 2; div < n; div++)
    	{
    		if(n % div == 0)
    			return 0;
    	}
    
    return 1;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    your modified code
    may be you want kind of this code

    Code:
    #include <stdio.h>
    
    int isprime(int n);
    
    #define EOL 10
    
    int main()
    {
      int x, col = 0;
      int print_count = 0;
      for ( x = 1; x <= 1001; x++)
        {
          if( isprime(x) == 1) {
            printf("%d\t", x);
            ++print_count;
            if(print_count == 10) {
              printf("\n");
              print_count = 0;
            }
          }
        }
      printf("\n");
    }
    
    int isprime(int n)
    {
      int div;
    
      if(n == 2)
        return 1;
    
      for(div = 2; div < n; div++)
        {
          if(n % div == 0)
            return 0;
        }
    
      return 1;
    
    }

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Perfect! I did some modifying since I had to use modulus but you pointed me in the right direction. Can you explain what #define EOL 10 is for?

    Code:
    #include "stdafx.h"
    
    int isprime(int n);
    
    int main()
    {
      int x, count = 0;
      for ( x = 2; x <= 1001; x++)
        {
          if( isprime(x) == 1) {
            printf("%d\t", x);
            ++count;
            if(count % 10 == 0) {
              printf("\n");
              count = 0;
            }
          }
        }
      printf("\n");
    }
    
    int isprime(int n)
    {
      int div;
    
      if(n == 2)
        return 1;
    
      for(div = 2; div < n; div++)
        {
          if(n % div == 0)
            return 0;
        }
    
      return 1;
    
    }

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    #define is a macro and it is a pre processor

    means before compilation begins the compiler will replace the EOL with its value 10

    simple did u understand or not

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Got it. Thanks.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    You are most Welcome Pal enjoy C/C++ programming

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RockyMarrone
    #define is a macro and it is a pre processor

    means before compilation begins the compiler will replace the EOL with its value 10
    That still does not explain what EOL is for though

    mrcg, although it helped you learn something new, in actual fact you should just remove that line since EOL is never used. Presumably RockyMarrone intended to use it as a named constant for the newline character, but here it is unnecessary since \n will do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Yeah, I was wondering what EOL stood for, then it just popped in my head. End of Line. I know what pre processors do and was just trying to figure out why he inserted in. Nonetheless, I thanked him for the help. This forum is awesome! Looking forward to contribute in the near future!

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by laserlight View Post
    That still does not explain what EOL is for though

    mrcg, although it helped you learn something new, in actual fact you should just remove that line since EOL is never used. Presumably RockyMarrone intended to use it as a named constant for the newline character, but here it is unnecessary since \n will do.

    No laser light my intent was not to use EOL as \n

    if you read my code carefully i want to use it at

    if (print_count == EOL)

    but i forgot to replace 10 as EOL

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RockyMarrone
    No laser light my intent was not to use EOL as \n

    if you read my code carefully i want to use it at

    if (print_count == EOL)

    but i forgot to replace 10 as EOL
    In that case perhaps PRIMES_PER_LINE would be a better name than EOL.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Ok you can do that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM