Ads 468x60px

Friday 16 December 2011

C and C++ program to generate first N prime numbers

In this article I am going to post a C+ program which will generate first N prime numbers where N will be obtained as a input from the user. If there is a wrong input, the program will display the error message. The coding of program goes as follows:
// Program to find the prime numbers in between n #include #include #include void main() { int n, i, flag; clrscr(); cout << "\n\t\t Enter any number => "; cin >> n; if (n < 5) { cout << "Your input is less than 5"; getch(); return; } i = 2; int j = 2; while (j < n-1 ) { flag = 0; i = 2; while (i< j ) { if(j%i == 0) { flag = 1; break; } i++; } if(flag == 0) cout<<"The number is " << j << endl; j++; } }

No comments:

Post a Comment