![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 1
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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 ![]() |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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;
}
__________________
"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 |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
I didn't know what the call for prime numbers were, that's why I couldn't write the prog.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|