Skip to main content

A C++ program to input an integer 'n' and get the 'n'th prime number

#include <iostream>
using namespace std;
int main()
{
        int n;
        cout<<"Input integer for n: ";
        cin>>n;
        int prime=0;
        int count,count1=0;
                for(int x=1;;x++){
                        count1=0;
                        for(int y=1;y<=x;y++)
                        {
                                if(x%y==0)
                                {
                                        count1++;
                                }
                        }
                        if(count1==2)
                        {
                                count++;
                                prime=x;
                        }
                        if(count==n)
                        {
                                break;
                        }
                }
        cout<<"Prime number is "<<prime<<endl;
        return 0;
}

OUTPUT:



Comments