Advertisement
axyd

C++_PrimeFinder

Apr 15th, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void isPrime(int, int &);
  6.  
  7. int main()
  8. {
  9. int out = 0, in = 0; //out= give to function, in=fetch from function
  10. char loop = 'y';
  11. while (loop == 'y' || loop == 'Y'){
  12. cout << "\nEnter a number and I will determine if it is prime.\n";
  13. cin >> out;
  14. isPrime(out, in);
  15.  
  16. if (in == 0)
  17. cout << "\n\nThis is not a prime number.\n";
  18. else
  19. cout << "\n\nThis is a prime number.\n";
  20. cout << "\nEnter (y|Y) to check another number. Anything else for 23.\n";
  21. cin >> loop;
  22. }
  23.  
  24. ofstream outputFile;
  25. outputFile.open("PrimeNumList.txt");
  26. int in2;
  27. for (int x = 1; x <= 100; x++)
  28. {
  29. isPrime(x, in2);
  30. if ( in2 > 0)
  31. outputFile << x << "\n";
  32. }
  33. outputFile.close();
  34. cout << "Prime numbers 1-100 now in file.\n";
  35.  
  36. system("pause");
  37. return 0;
  38. }
  39. void isPrime(int num1, int &num2) //input, output
  40. {
  41. if (num1 < 2) //No primes below 2
  42. num2 = 0;
  43. else if (num1 == 2) //2 =prime
  44. num2 = 1;
  45. else{
  46. for (int i = 2; i < num1; i++) //cant be equal to number
  47. {
  48. num2 = (num1 % i);
  49. if (num2 == 0)
  50. break;
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement