TechArena Community To write first 100 prime numbers in c++ program.

#1
09-07-2009
 Member Join Date: Jul 2009 Posts: 124
To write first 100 prime numbers in c++ program.

Hello friends,

I want a simple program to write 100 prime numbers in c++

How to write c++ program the first 100 prime numbers.

#2
09-07-2009
 Member Join Date: Oct 2008 Posts: 79
Re: To write first 100 prime numbers in c++ program.

Hey,

Why dont you use a for loop?

Code:
`for ( int n = 0, n >= 100, n++)`
here it is:
Code:
```#include <iostream>
using namespace std;

int main() {

for (int n = 3; n <= 100; n++) {
// TO DO
}

return 0;
}```
I hope this helps.
#3
09-07-2009
 Member Join Date: Oct 2008 Posts: 65
Re: To write first 100 prime numbers in c++ program.

Hey I have the code but its for java. The logic is same.
Try it. It give you first 100 prime numbers.
Code:
```public class whileprime
{
public static void main(String args[])
{
int num = 4;
int count = 0;
System.out.println("First 100 Prime Numbers: ");
System.out.println("1");
System.out.println("2");
System.out.println("3");
while(count < 100)
{
num++;
if(num % 2 != 0)
{
int dv = 3;
while((num % dv != 0)&&(dv <= ((num - 1) / 2)))
{
dv++;
}
if(num % dv != 0)
{
count++;
System.out.println(num + "         ");

}
}
}

}

}```
I hope this helps you.
#4
09-07-2009
 Member Join Date: Oct 2008 Posts: 86
Re: To write first 100 prime numbers in c++ program.

Here is a code to find out prime numbers between 1 - 100 :

Code:
```void main()
{
int i,number=1;
clrscr();
while(number<=100)
{
i=2;
while(i<=number)
{
if(number%i==0)
break;
i++;
}
if(i==number)
printf("\n%d is Prime",number);
number++;
}
getch();
}```
Is this what you need?

 Tags: