fork(1) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i, n;
  8.  
  9. // read n from the user
  10.  
  11. cin >> n;
  12.  
  13. // print out all numbers <=n that are divisible by 3
  14.  
  15. // approach 1
  16.  
  17. cout << "Approach 1:" << endl;
  18.  
  19. for (i = 1; i <= n; i++)
  20. if (i%3 == 0)
  21. cout << i << endl;
  22.  
  23. // approach 2
  24.  
  25. cout << "Approach 2:" << endl;
  26.  
  27. for (i = n; i > 0; i--)
  28. if (i%3 == 0)
  29. cout << i << endl;
  30.  
  31. // approach 3
  32.  
  33. cout << "Approach 3:" << endl;
  34.  
  35. for (i = 3; i <= n; i+=3)
  36. cout << i << endl;
  37.  
  38. // approach 4
  39.  
  40. cout << "Approach 4:" << endl;
  41.  
  42. for (i = n-n%3; i > 0; i-=3)
  43. cout << i << endl;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
Standard output is empty