fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int getClosestPrime(int inputNum){
  6. bool isPrime = false;
  7. inputNum++;
  8. while (!isPrime){
  9. for (int i = 2; i <= inputNum/2; i++){
  10. if (inputNum % i == 0){
  11. isPrime = false;
  12. break; //if at any time found it's not a prime, break the for loop
  13. }
  14. isPrime = true;
  15. }
  16. if (isPrime == false){
  17. inputNum++;
  18. } else {
  19. return inputNum;
  20. }
  21. }
  22. }
  23.  
  24. int main()
  25. {
  26. int a = getClosestPrime(1258);
  27. cout <<"1259?"<<endl;
  28. cout <<a<<endl;
  29. a = getClosestPrime(1259);
  30. cout <<"1277?"<<endl;
  31. cout <<a;
  32. }
  33.  
Success #stdin #stdout 0s 4408KB
stdin
Standard input is empty
stdout
1259?
1259
1277?
1277