Q:

Write a function that interacts with the user, asking for a number and generating the factorial of that number. Call this function from main

0

Write a function that interacts with the user, asking for a number and generating the factorial of that number. Call this function from main.

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;
  while (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