Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Prime Numbers In C (http://www.programmingforums.org/showthread.php?t=1006)

Cloudbreak Nov 1st, 2004 9:28 AM

Hello, I'm quite new to the C language and I wanted to try and make a program that'll input a number from the user and tell him whether it is prime or not.
This is the program I tried to build:

Quote:

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int i, number, square;

printf("Enter a potential prime:\n");
scanf("%d", &number);

square = sqrt(number);

for (i=2; i < square; i++)
{
if(number%i==0)
printf("Number is prime.\n");
else
printf("Number is not prime because it is divisible by %d", i);
}

getch();
return 0;
}*

No matter what number I input it tells me it is divisible by 2. Please help me if you know how to fix it...

Thanks ahead.
Cloudbreak.

Pizentios Nov 1st, 2004 10:57 AM

First off, last time i checked a prime number was defined like this:

A prime number is a positive integer that has exactly two positive integer factors, 1 and itself. For example, if we list the factors of 28, we have 1, 2, 4, 7, 14, and 28. That's six factors. If we list the factors of 29, we only have 1 and 29. That's 2. So we say that 29 is a prime number, but 28 isn't.Another way of saying this is that a prime number is a whole number that is not the product of two smaller numbers. Note that the definition of a prime number doesn't allow 1 to be a prime number: 1 only has one factor, namely 1. Prime numbers have exactly two factors, not "at most two" or anything like that. When a number has more than two factors it is called a composite number.

:

#include <iostream.h>

int main()
{
        int num;
        cout << "\nPlease enter a number to check: ";
        cin >> num;
       
        if ((num%2) == 0)
        {
 cout << "\nNumber is not prime.\n";
        }
        else
        {
 cout << "\nNumber is prime.\n";
        }
        return 0;
}


This is how i'd do it. works for every prime except 2. if you want a list of prime numbers to test with there is a great list here. It's the first 1000 prime numbers. I think you were getting mixed up witht eh square root part, you really don't need it unless your trying to output a whole bunch of prime numbers....not checking for just one. I could be wrong though. Can somebody back me up on this?

Ooble Nov 1st, 2004 11:36 AM

That above example would state that numbers such as 9, 15 and 27 were prime - they're not. If you want to check primes, try the following:

:

int prime = 1;

for (int i = 0; i < sqrt(number); i++)
{
  if ((number / i) == (int)(number / i))
    /* i is a factor - the number divided by i is the same when converted to an integer. */
    prime = 0;
}

if (prime == 0)
  printf("%i is not a prime number.", number);
else
  printf("%i is a prime number.", number);


Not tested, but it should work.

Mjordan2nd Nov 1st, 2004 4:12 PM

Here's how I did it:

:

#include <stdio.h>
#define TRUE 1
#define FALSE 0

int main(void)
{
        printf("enter possible prime number");
        int number;
        scanf("%d", &number);
       
        int prime = TRUE;
        int i;
        for(i = 2; i<number; i++)
        {
 if(number%i==0)
 {
        prime = FALSE;
 }
        }
        if(number == 2)
        {
 prime = TRUE;
        }
       
        if(prime)
        {
 printf("%d is a prime number", number);
        }
        else
        {
 printf("%d isn't a prime number", number);
        }
        return 0;
}


Pizentios Nov 2nd, 2004 9:56 AM

Quote:


That above example would state that numbers such as 9, 15 and 27 were prime - they're not.

Correct, i didn't do much testing on that code. Heh, now i look like an idiot.

Ooble Nov 2nd, 2004 11:48 AM

Mine's slightly buggered - it should be:
:

  if ((number / i == (int)(number / i)) && i != 1 && i != number)
    prime = 0;
    /* i is a factor that is not 1 or the number itself - the number divided by i is the same when converted to an integer. */
  if (number == 1)
    prime = 0;
    /* 1 is not a prime number */

If prime is still 1 after all that, the number's a prime.


All times are GMT -5. The time now is 7:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC