fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // f is just a simple user-defined function
  6.  
  7. int f(int n)
  8. {
  9. return 2*n-1;
  10. }
  11.  
  12. // factorial computers the factorial of n
  13. // equal to the product 1*2*3*...*n
  14.  
  15. int factorial(int n)
  16. {
  17. int product = 1;
  18.  
  19. for (int i=2; i<=n; i++)
  20. product = product * i;
  21.  
  22. return product;
  23. }
  24.  
  25. // is_prime returns 1 is n is a prime,
  26. // otherwise it returns 0.
  27.  
  28. int is_prime(int n)
  29. {
  30. // anything up to 1 cannot be a prime
  31.  
  32. if (n<=1)
  33. return 0;
  34.  
  35. // 2 is a prime
  36.  
  37. if (n==2)
  38. return 1;
  39.  
  40. // try to divide by i=2 to n-1,
  41. // if divisible, n is not a prime
  42.  
  43. for (int i=2; i<n; i++)
  44. if (n%i==0)
  45. return 0;
  46.  
  47. // if we got this far, the number is a prime
  48.  
  49. return 1;
  50. }
  51.  
  52. int main()
  53. {
  54. int nmin = 1;
  55. int nmax = 10;
  56. int nstep = 1;
  57.  
  58. for (int n = nmin; n <= nmax; n = n + nstep)
  59. {
  60. cout << "n = " << n << endl;
  61. cout << " f(" << n << ") = " << f(n) << endl;
  62. cout << " factorial(" << n << ") = " << factorial(n) << endl;
  63. cout << " is_prime(" << n << ") = " << is_prime(n) << endl;
  64. }
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
n = 1
   f(1) = 1
   factorial(1) = 1
   is_prime(1) = 0
n = 2
   f(2) = 3
   factorial(2) = 2
   is_prime(2) = 1
n = 3
   f(3) = 5
   factorial(3) = 6
   is_prime(3) = 1
n = 4
   f(4) = 7
   factorial(4) = 24
   is_prime(4) = 0
n = 5
   f(5) = 9
   factorial(5) = 120
   is_prime(5) = 1
n = 6
   f(6) = 11
   factorial(6) = 720
   is_prime(6) = 0
n = 7
   f(7) = 13
   factorial(7) = 5040
   is_prime(7) = 1
n = 8
   f(8) = 15
   factorial(8) = 40320
   is_prime(8) = 0
n = 9
   f(9) = 17
   factorial(9) = 362880
   is_prime(9) = 0
n = 10
   f(10) = 19
   factorial(10) = 3628800
   is_prime(10) = 0