![]() |
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. |
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>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? |
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;Not tested, but it should work. |
Here's how I did it:
:
#include <stdio.h> |
Quote:
|
Mine's slightly buggered - it should be:
:
if ((number / i == (int)(number / i)) && i != 1 && i != number) |
| 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