Start New Discussion within our Software Development Community
#include <iostream>

using std::endl;
using std::cout;
using std::cin;

int main()
{
	long long i=0, j=0,k=0,m=0;
	cout << "which primary number you want to get .? " << endl;
	cin >> k;

	bool isPrime = false;
	cout << "Prime Number : " << endl;

	for(i = 2 ; i<= 999999 ; i++)
	{
		for(j = 2; j <= 999999 ; j++)
		{
			if(i != j && 0 == i % j)
			{
				isPrime = false;
				break;
			}else
			{
				isPrime = true;
			}
		}

		if(isPrime == true)
		{
			if(m!=k){
			m++;
			cout << "(" << m << ") " << i << endl;
			}
		}
	}

	cout << endl;
	system("pause");
	return 0;
}

Copy and Paste then Run :)

1)ISO C++ does not support 'long long'
2)Use functions, it makes it easier and clearer
3)No need for system("pause"), either let the IDE handle that, or use better options
4)Rename your variable to something more appropriate.
5)Why 999999? Why do you limit it to that?

Compare with this :

#include <iostream>
#include <string>
#include <limits>

using std::string;
using std::endl;
using std::cout;
using std::cin;

bool isPrime(int num){
	if(num < 2) return false;
	if(num == 2) return true;
	if(num % 2 == 0) return false;

	for(int i = 3; i < num/2; i += 2){
		if(num % i == 0) return false;
	}
	return true;
}
void pauseState(){
	cin.clear();	
	cin.ignore( std::numeric_limits<std::streamsize>::max(),'\n');
	cout << "\nPress enter to exit...";
	getline(cin,string());
}
int main()
{
	
	cout << "Enter the limit of prime number to generate " << endl;	
	size_t max = 0;
	cin >> max;

	for(int i = 2, count = 0;  count != max; ++i){
		if(isPrime(i)){
			cout << ++count << ") " << i << " is prime\n";
		}
	}

	pauseState();

	return 0;
}
This article has been dead for over six months. Start a new discussion instead.