axyd icon

C++_PrimeFinder

axyd | PRO | 04/16/16 01:47:17 AM UTC | 0 ⭐ | 787 👁️ | Never ⏰ | []
text |

1.11 KB

|

None

|

0 👍

/

0 👎

#include <iostream> 
#include <fstream> 
using namespace std;
 void isPrime(int, int &);
 int main()
{
	int out = 0, in = 0; //out= give to function, in=fetch from function
	char loop = 'y';
	while (loop == 'y' || loop == 'Y'){
		cout << "\nEnter a number and I will determine if it is prime.\n";
		cin >> out;
		isPrime(out, in);
 		if (in == 0)
			cout << "\n\nThis is not a prime number.\n";
		else
			cout << "\n\nThis is a prime number.\n";
		cout << "\nEnter (y|Y) to check another number. Anything else for 23.\n";
		cin >> loop;
	}
 	ofstream outputFile;
	outputFile.open("PrimeNumList.txt");
	int in2;
	for (int x = 1; x <= 100; x++)
	{
		isPrime(x, in2);
		if ( in2 > 0)
			outputFile << x << "\n";
	}
	outputFile.close();
	cout << "Prime numbers 1-100 now in file.\n";
 	system("pause");
	return 0;
}
void isPrime(int num1, int &num2) //input, output
{
	if (num1 < 2) //No primes below 2
		num2 = 0;
	else if (num1 == 2) //2 =prime
		num2 = 1;
	else{
		for (int i = 2; i < num1; i++) //cant be equal to number
		{
			num2 = (num1 % i);
			if (num2 == 0)
				break;
		}
	}
}

Comments