Program to print the first N Prime Nos. in C++

//First N Prime Nos. in C++

#include<iostream>
#include<iomanip>
using namespace std; 

int main()
{
int i,j,num,flag=0,cnt=0;
cout<<"Enter num : ";
cin>>num;
cout<<"\nFirst "<<num<<" prime numbers are:\n";
for(i=1;cnt<=num;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag++;
}
}
if(flag==0)
{
cout<<setw(4)<<i;
cnt++;
}
}
}


No comments

Post your comments

Powered by Blogger.