1. ## prime no's

Hi!.This is my basic program to display Prime no's from 2 to 100.
I tried for weeks to get the algorithm correct. Still the code does have some problems. It prints all the numbers from 2 to 200.
Glad if someone cud help to solve my problem.

*************************code********************* ******************
Code:
```/* All prime numbers between 2 to 100 */

#include<stdio.h>

void main()
{

int num,i=2,rem;
for(num=2;num<100;num++)

{

while(i<num)
{

rem=num%i;
i++;
if(rem!=0) break;

}

if(rem!=0)
printf("%d\t",num);

}

}

*********************************************************************```

Thanks
Mur

2. Search the board. This gets posted every week. No, wait, it gets posted every day. See down about ten posts?

Quzah.

3. ## Re: prime no's

Originally posted by mur3
void main
/me cringes in fear

does it bite?

4. Q) What is a prime number?
A) A prime number is a number with the factors of only one and itself.

Q) What is the simplest way to find prime numbers on a computer?
A) Use a for loop and go through every number until the square root of the number in question. (Half that number would work fine as well). Check each number to determine if it is a factor.
Code:
`if (number_in_question % possible_factor == 0) ...`
If so, then you know for sure that your number is not prime.

You're over-thinking the problem.

5. Furthermore, you can cut your list in half by simply ignoring everything that is even numbered. Thus:

for( x = 1; x < 100; x += 2 )

This skips every even number.

Quzah.

6. A simple search would of solved this problem. Does anyone ever use that I wonder.

7. thanks guys..
really appreciate it..

Mur..

8. Then there is the most reasonable solution:
Code:
```#include <stdio.h>

#define TABLE_SIZE(table) sizeof(table) / sizeof(table[0])

static const int prime_table[] = {
/* Empty element */0,
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
};

int main ( void )
{
int index;
puts ( "Prime numbers from 2 to 100:" );
for ( index = 1; index < TABLE_SIZE ( prime_table ); index++ )
printf ( "%-3d%c", prime_table[index], ( index % 5 == 0 ) ? '\n' : ' ' );
return 0;
}```
-Prelude