Sample – factorial

by Nideesh C on February 8, 2011 · 0 comments

in C++




#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);
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample program (multiplication using addition)
  2. Sample – Fibonacci numbers
  3. Sample – count number of characters and lines in a file
  4. Stereo FM Transmitter Using I.C – BH1417

Leave a Comment

Previous post:

Next post: