Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Mar 19th, 2005, 7:40 PM   #1
BehemothPhoenix
Newbie
 
Join Date: Mar 2005
Posts: 1
Rep Power: 0 BehemothPhoenix is on a distinguished road
Angry Help with a couple prime numbers programs!!

Basically, I have to write a program for school, in which I have to determine whether a user inputted value is prime or not. I also have to write another program determining what factors of an inputted value are prime. I've tried everything (and we can't use the math.h library). I'm really not too sure what to do, and any help you could give me would be greatly appreciated!!

Edit: I don't really need a whole program, just an idea of where to start.
BehemothPhoenix is offline   Reply With Quote
Old Mar 20th, 2005, 12:12 AM   #2
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
hmmm... well a long way would be to incorporate all the prime numbers into a function, and if it matches that in the function then it's prime (from the number the user entered)

really long time to make something like that, or...





cheat and write the mat.h library header into a function and use it in your assignment
gardon is offline   Reply With Quote
Old Mar 20th, 2005, 1:55 AM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
It's not a tough program at all. Write a function to determine whether it's prime or not. In the function, have an int variable which stores whether the number is prime or not -- 1 for prime and 0 for not prime. Initialize it to 1. Have it run through a loop from 2 up to 1 below the number you're testing for. If it's evenly divisible, then set the value to 0 . . .

 
 #include <stdio.h>
 
 #define PRIME 1
 #define NOT_PRIME 0
 
 int main(void)
 {
 	 // some code
 	 // ...
 	 // ...
 	 int isPrime = checkPrime(number);
 }
 
 int checkPrime(int num)
 {
 	 int value = PRIME;
 	 int i = 2;
 	 for(i; i<num; i++)
 	 {
 		  if(i%num==0)
 			   value = NOT_PRIME;
 	 }
 	 return value;
 }
__________________
&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 Mar 20th, 2005, 3:06 PM   #4
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
I didn't know what the call for prime numbers were, that's why I couldn't write the prog.
gardon 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 3:41 AM.

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