Prime numbers from 1 to 100

Hey there, need some assistance. I want to print out prime numbers from 1 to 100. I've created a program but the output is this.
5
7
11
13
17
19
23
25
29
31
35
37
41
43
47
49
53
55
59
61
65
67
71
73
77
79
83
85
89
91
95
97

here is the source code: Im using DEV C++
#include<stdio.h>
#include<conio.h>
main()
{
int num1;
for(num1=2;num1<=97;num1++)
{
if((num1%2!=0)&&(num1%3!=0))
printf("\n%d",num1);
}

getch();
}

is there something wrong???
closed account (zwA4jE8b)
you should '%' the number you are testing with all the numbers up until 'number/2'

that way it tests all the possibilities for multiples
Last edited on
Yes, what you are using is called the "Sieve of Eratosthenes", but you are only sieving multiples of 3 and 5. You must also sieve multiples of all other prime numbers < sqrt(100).

See http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Good luck!
closed account (zwA4jE8b)
oh sqrt() that is much less trials than n/2. Thanks, I never knew that
an alternate method:
main()
{
int num1;
for(num1=2; num1<=97; num1++)
{
int x=2, prime=1;
while(x<num1 && prime==1) // USING WHILE LOOP INSIDE FOR LOOP
{
if(num1%x==0)
prime=0;
else
x++;
}
if(prime==1)
cout<<num1<<endl;
}
}
#include <iostream>
using namespace std;
#include <process.h>
int main()
{
unsigned long n,j;
cout<<"enter a number:";
cin>>n;
for(j=2;j<=n/2;j++)
if (n%j ==0)
{
cout<<"its not a prime:";
exit(0);
}
cout<<"its a prime";
cout<<endl;
return 0;
}
this is for all the prime numbers.
Topic archived. No new replies allowed.