Now implementing this logic of **Prime Numbers** into our c++ code, we need simple for loops and conditional statements. This is very easy to implement and print **Prime Numbers** upto 1000. Lets have a look at the following code:

- //Prime Numbers generation in C++
- //Using for loops and conditional structures
- #include <iostream>
- using namespace std;
- int main()
- {
- int a = 2; //start from 2
- long long int b = 1000; //ends at 1000
- for (int i = a; i <= b; i++)
- {
- for (int j = 2; j <= i; j++)
- {
- if (!(i%j)>>(i!=j)) //Condition for not prime
- {
- break;
- }
- if (j==i) //condition for Prime Numbers
- {
- cout << i << endl;
- }
- }
- }
- }

Now lets explain this code, first of all we defined two variables that defined our limits upto which we are going to print **Prime Numbers**. The most important thing is using the “conditional statement ” inside the nested for loops. We used a condition that if the variables inside the loop do not have true modulus and both the variables are not equal then break, otherwise if both variables are equal then it is a **Prime Number**, and print it. And its all about** Generating** **Prime Numbers in C++**.