Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 1st, 2004, 8:28 AM   #1
Cloudbreak
Newbie
 
Join Date: Nov 2004
Posts: 5
Rep Power: 0 Cloudbreak is on a distinguished road
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.
Cloudbreak is offline   Reply With Quote
Old Nov 1st, 2004, 9:57 AM   #2
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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!
Pizentios is offline   Reply With Quote
Old Nov 1st, 2004, 10:36 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 1st, 2004, 3:12 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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;
}
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 2nd, 2004, 8:56 AM   #5
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
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.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Nov 2nd, 2004, 10:48 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:54 PM.

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