prime numbers re-posted

This is a discussion on prime numbers re-posted within the C Programming forums, part of the General Programming Boards category; Hello everyone, I finally figured out how to write the code I originally thought I was posting on the weekend. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    19

    prime numbers re-posted

    Hello everyone,

    I finally figured out how to write the code I originally thought I was posting
    on the weekend.

    The purpose of this small program is to identify prime numbers.

    It seems to work okay until a number is entered that is divisible by 11
    (or an elevenish type number). Example-- 33 is identified as prime,
    777 is identified as prime, etc.

    Any help or comments on this code will be appreciated.

    Here's the code. Thanks everyone.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int number;
    	printf("Enter a number:\n");
    	scanf("%d", &number);
    
    	int a;
    
    	for(a = 2; a < number; a++)
    	{
    		if((number % a) == 0)
    		{
    		printf("%d is not prime.\n", number);
    		break;		
    		}
    		
    		
    	       
    		if((number % a) != 0)
    		{
    		printf("%d is prime.\n", number);
    		break;
    		}
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    Thanks, however if using (number % a) == 0 should only tell me whether or not the number is divisible
    by a whole number in such a way that it will not leave a remainder. If the number is prime the remainder of
    (number / a) would not be 0.

    Am I missing something obvious?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    rather, you should look at your loop, specifically at your two if conditions.......your loop only executes once, no matter what number you enter

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    actually it won't work for any odd numbers that are not prime numbers - try 15, 21 and so on.

    your program checks if a number is divisible by two if it is it returns number is not prime if it isn't number is prime.

    the purpose of the for loop is to check how many factors there are of the number other than itself and 1. you need to let it finish its job and then check if it is a prime or not after it is finished- that is place the ifs after the end of the for block. you will also need something in the for block to count how many factors there are of the number aswell.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  4. More Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 3
    Last Post: 02-21-2003, 10:06 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM

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