Monday, 7 April 2014

A Program to Calculate Factorial of a Number

A Program to Calculate Factorial of a Number

#include <iostream.h>

int fact (int i) {
  int result = 1;
  while (i > 0) {
    result = result * i;
    i = i-1;
  }
  return(result);
}

int main () {
  int n;
  cout << "Enter a natural number: ";
  cin >> n;
  while (n < 0) {
    cout << "Please re-enter: ";
    cin >> n;
  }
  cout << n << "! = " << fact(n) << endl;
  return(0);
}

No comments:

Comment

Comment Box is loading comments...