fork download
  1. #include <iostream>
  2. #define PROMPT "Please enter a whole number:"
  3. #define NOT_PRIME "The number is not a prime number.\n"
  4. #define PRIME "The number is a prime number.\n"
  5. #define DONE 0 /* ends successful program */
  6. #define FIRST_FACTOR 2 /* initial value in for loop */
  7. using std::cout;
  8. using std::cin;
  9. int main(){
  10. int i; /* loop counter */
  11. int number; /* number provided by user */
  12. cout << PROMPT; /* promt user */
  13. cin >> number; /* wait for user input */
  14. /* Prime numbers are defined as any number
  15. * greater than one that is only divisible
  16. * by one and itself. Dividing the number
  17. * by two shortens the time it takes to
  18. * complete. */
  19. for(i = FIRST_FACTOR; i <= number/2; ++i)
  20. if( number%i == 0 ){ /* if divisible */
  21. cout << NOT_PRIME << number; /* not prime */
  22. return DONE; /* exit program */
  23. }
  24. /* if number is not divisible by anything
  25. * than it must be prime */
  26. cout << PRIME << number;
  27. return 0; /* exit program */
  28. }
  29.  
Success #stdin #stdout 0s 2856KB
stdin
33
stdout
Please enter a whole number:The number is not a prime number.
33