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

#1
 supernoob 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
 Corona 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
 Rock Villa 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
 Cadallic 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:

 Thread Tools Search this Thread Show Printable Version Email this Page Search this Thread: Advanced Search Similar Threads for: "To write first 100 prime numbers in c++ program." Thread Thread Starter Forum Replies Last Post Program to print GCD of given numbers Bhavesh_seth Software Development 5 15-03-2011 08:03 PM Need C program code to count numbers of negative & positive numbers Sarfaraj Khan Software Development 5 16-01-2010 02:00 PM Program to print odd and even numbers roodiii Software Development 3 30-11-2009 02:13 PM What is the program to swap two numbers? Bharat89 Software Development 3 26-11-2009 09:37 AM Write numbers in arabic Neider Off Topic Chat 3 21-08-2009 06:37 PM

All times are GMT +5.5. The time now is 04:29 AM.