Q:

Write and test your own version of fact

0

 Write and test your own version of fact.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>

int fact(int n) {  // Ignore overflow
  if (n < 1) return n;
  int k = n;
  while (--n)
    k *= n;
  return k;
}

int main() {
  int n;
  std::cin >> n;
  std::cout << n << "! = " << fact(n) << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now