prime program

hey can anyone help me with this program, I've been trying to figure it but i haven't been able to understand it:



An integer is said to be prime if it is divisible only by 1 and itself.
a. write a function that determines if a number is prime. Send in a number that you will be testing. Return 1 if it is prime, 0 if it is not.
b. Use this function in a program that determines and prints all the prime numbers between 1 and 10,000.

i did this code but i don't know what to do after that or if its even right so far:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <prime.h>

int prime(int x);

int main()
{
int x,z,sum;
for (z=1;z<=10000;z=z+1)
{
printf("%d",prime(y))
{




system("PAUSE");

return 0;

}
int prime(int x)
{
int prime;
prime =



You should assume at first that it is prime, then you should go through and check every number between and including 2 and its square root to see if they go into the number without a remainder. If there is a remainder you immediately know it is not prime and can stop checking for factors. After all factors have been checked, you know that it is actually prime.
can you be able to illustrated in code, i understand better thhat way
In pseudocode:
1
2
3
4
5
isprime = true;
while i = 0, i < sqrt(number), ++i
	if i goes evenly into number
		it is not prime, exit
it is prime, return 1


You can add little performance test too, like checking if the last digit of a number is even before the test (and return 0 if it is even, obviously).
Last edited on
Topic archived. No new replies allowed.