fork(1) download
  1. #include <iostream>
  2. using namespace std ;
  3. int m , n , t,rem, num;
  4. int main(){
  5. cout << "Enter the natural number "<< '\n' ;
  6. cin >> m ;
  7. if (m==1 || m==2) {
  8. cout << m << "is prime" << '\n' ;
  9. //if cin is 1 or 2 it immediately tells me that they are prime.
  10.  
  11. }else{
  12. for (int i=3; i<m; i++) {
  13. for (n=2; n<i ; n++) {
  14. rem=i%n ;
  15. // for i<m the function checks the remainder of i when divided by numbers less than i.
  16. if (rem == 0) {
  17. break;
  18. }else{
  19. num=i ;
  20. }
  21. //If there is a remainder which is equal to 0 then the loop stops and goes further to check for another 'i'.
  22. }
  23. cout << num ;
  24.  
  25.  
  26. }
  27.  
  28. }
  29.  
  30. return 0 ;
  31. }
Success #stdin #stdout 0s 3144KB
stdin
Standard input is empty
stdout
Enter the natural number