C++

Code to display first n prime numbers in C++

Code to display first n prime numbers in C++

In this article, we will discuss the concept of Code to display first n prime numbers in C++

In this code, we are going to learn how to find first n prime numbers  using different methods in C++ language.

This is done using for loop,while loop and do-while loop in C++ language

Program to display first n prime numbers

Program to display first n prime numbers using for loop

In this program, we will display first n  prime numbers using for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int n,i=3,counter,num;
    cout<<"Enter the number of prime you want\n";
    //ask input from the user and store in n
    cin>>n;

    if(n>=1){
        cout<<"First %d prime numbers are: \n";
        cout<<"2 ";
    }
    for(counter=2; counter<=n; i++){
        for(num=2; num<i; num++){
            if(i%num==0)
                break;
        }
        if(num==i){
            cout<<i<<" ";
            counter++;
        }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the number of prime you want
6
First 6 prime numbers are :
2  3  5  7  11  13

 

Program to display first n prime numbers using while loop

In this program, we will display first n  prime numbers using while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int n,i=3,counter,num;
    cout<<"Enter the number of prime you want\n";
    //ask input from the user and store in n
    cin>>n;

    if(n>=1){
        cout<<"First "<<n<<"  prime numbers are: \n\n";
        cout<<"2 ";
    }
    counter=2;
   while(counter<=n){
        num=2;
       while(num<i){
            if(i%num==0)
                break;
    num++;
        }
        if(num==i){
            cout<<i<<" ";
            counter++;
        }
        i++;
    }
    getch();
    return 0;
}


When the above code is executed, it produces the following result

Enter the number of prime you want
15
First 15 prime numbers are :
2  3  5  7  11  13 17 19 23 29 31 41 43 47

 

Program to display first n prime numbers using do-while loop

In this program, we will display first n  prime numbers using do-while loop in C++ language

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int n,i=3,counter,num;
    cout<<"Enter the number of prime you want\n";
    //ask input from the user and store in n
    cin>>n;

    if(n>=1){
        cout<<"First "<<n<< " prime numbers are: \n\n";
        cout<<"2 ";
    }
    counter=2;
   do{
        num=2;
       do{
            if(i%num==0)
                break;
    num++;
        }while(num<i);
        if(num==i){
            cout<<i<<" ";
            counter++;
        }
        i++;
    }while(counter<=n);
    getch();
    return 0;
}



When the above code is executed, it produces the following result

Enter the number of prime you want
10
First 15 prime numbers are :
2  3  5  7  11  13 17 19 23 29

 

Suggested post

Operator in C++ language

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.