C++ Program to find nth Prime number

ad+1

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

int main()
{
   int check,n=0,c=0;

   cout<<"Enter Prime Number you want to Find: ";
   cin>>n;
  
  for(int i=2;i<=1000;i++)
  {
    check=0;
   
    for(int j=2;j<=i/2;j++)
    {
        if(i%j==0)
        {
          check=1;
           break;
        }    
    }
   
  if(check==0)
    c++;

      if(c==n)
     {
      cout<<n  <<" Prime Number is: " <<i;
      break;
     }
 }

return 0;

}


Output:

 

7 comments: Leave Your Comments

  1. sir why this program dose not execute 10001th prime number???

    ReplyDelete
  2. THANK YOU, THIS PROGRAM HELPS ME TO FINISH MY MIPS PROGRAM HOMEWORK

    ReplyDelete
  3. This code is helpful but I want some Example of C++ Programs;

    Print Star pattern in C++
    Print number pattern in C++
    Prime number program in C++
    Factorial Program in C++

    ReplyDelete
  4. I found nice article in your blog.thank you for useful info
    .

    ReplyDelete
  5. Can anyone tell me why is it necessary to initialize check variable inside for loop only...
    Because in my program I had written check variable outside and before for loop but it did not give me the output

    ReplyDelete