Thread: Need Some Coaching, Display 100 Prime Numbers

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Need Some Coaching, Display 100 Prime Numbers

    I've been working at this for awile and I need some help. I can print the first 100 non-prime numbers but not the first 100. What am I missing? This nested loop business is killing me. Here is what I have:

    Code:
    #include<stdio.h>
    
    int main (void)
    {
         int num, test;
    	 int prime = 0;
    	 int not_prime = 0;
    
    	 for (num = 2; num < 100; num++)
    	 {
    		
    		 for (test = 2; test < (num - 1); test++)
    			
    		{
    					if (num % test == 0)
    				{
    					printf ("%d\n", num);
    					break;
    				}
    		 }
    		//printf ("%d\n", num);	
    			
    		}
    	 }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    I can print the first 100 non-prime numbers but not the first 100
    Huh?

    Also, clean up your whitespace. Makes it easier to read...for everyone!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    76
    Not the first 100 prime.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Do you want to print out non-prime numbers? I don't know what your requirements are. You never stated them clearly.

    Regardless, you need to work out on paper how you'd do this as if you were a computer. Flowchart it , then write the code.

    Throwing loops together haphazardly is a recipe for disaster.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    I want to print the first 100 prime numbers. I am able to do the all of the non primes from 0 to 100 but cannot figure out how to get the primes. This is what I think I need to do.

    Make a loop that goes from 2 to 100.
    Make another loop that will test each value by using modulus 2 to the number -1
    If any 0's are found, the number is not prime
    If no zeroes are found the number is prime

    I can't figure out how to catch the primes. Actually I have caught them a couple times but they print for every trip through the loop. I can;t figure out how to just catch them one at a time.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    See bold
    Quote Originally Posted by dolfaniss View Post

    Make a loop that goes from 2 to 100.
    Make another loop that will test each value by using modulus 2 to the number -1
    If any 0's are found, the number is not prime <---set a flag variable (say, prime_found ) and break from the loop either directly, or by the test at the top of the loop
    If no zeroes are found the number is prime

    I can't figure out how to catch the primes. Actually I have caught them a couple times but they print for every trip through the loop. I can;t figure out how to just catch them one at a time.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Something like this (not tested)
    Code:
        prime_found = FALSE;
        for (test = 2; test < (num - 1); test++) {
            if (num % test == 0) {
                prime_found = TRUE;
                break;
            }
        }
        if (prime_found) {
            printf ("%d\n", num);
        }

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    76
    Thanks Mike, I will play around and see what I can come up with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime numbers
    By cubbies5 in forum C++ Programming
    Replies: 11
    Last Post: 03-30-2011, 09:19 AM
  2. Prime numbers in asm
    By aestetics in forum Tech Board
    Replies: 4
    Last Post: 03-22-2011, 02:22 AM
  3. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  4. prime numbers
    By Murdogg7 in forum C Programming
    Replies: 1
    Last Post: 09-25-2002, 03:04 AM
  5. Prime numbers
    By Lazy Student in forum C Programming
    Replies: 12
    Last Post: 05-14-2002, 05:53 AM