![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Newbie
Join Date: Nov 2004
Posts: 5
Rep Power: 0
![]() |
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:
Thanks ahead. Cloudbreak. |
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() |
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?
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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;
}
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() ![]() |
Quote:
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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 */ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|