Prime Number Function

This is a discussion on Prime Number Function within the C Programming forums, part of the General Programming Boards category; Hey guys. I'm new to the forums and C, but I think I'm learning quickly. I think I got the ...

  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
    21,594
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,594
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21