# Thread: Help with program code re prime numbers

1. ## Help with program code re prime numbers

Please help! I'm trying to write a function that determines if a number is prime to use this function to determine and prints all prime numbers between 1 and 100. This is what I have so far:

#include <stdio.h>

int prime(int num)
{

int sw = 1;
int count:

}

for( count = num-1; count >=2; count--);

scanf("%d", &prime);

printf("%d\n", count, sw=1);
return 0;
}

2. I could help you with logic. May be u can implement it easily if you know some looping concept.

If you divide the number from 2 to (number-1), you should get some reminder then that number is prime.

FYI: 2 is the only even prime number.

Bye,
Juganoo

3. ## Code re Prime Numbers

Thank you Juganoo for your help! I appreciate you!

4. The code of vVv is almost correct, but it also gives 1 as a prime number, which is incorrect. A number is prime if it has two divisors, 1 has only one divisor. So what you could do is count the number of divisors of a number, if it equals 2, then it is prime.

Code:
```int nr_of_divisors (int n)
{
int i, r = 0;

for (i = 1; i <= n; i++) {
if ((n % i) == 0)
r++;
}

return r;
}```