+ 6

Find the factorial of a number??

#include <iostream> using namespace std; int main() { int number, i = 1, factorial = 1; cout << "Enter a positive integer: "; cin >> number; while ( i <= number) { factorial *= i; ++i; } cout<<"Factorial of "<< number <<" = "<< factorial; return 0; }

4th Dec 2016, 2:30 PM
chandni singh
chandni singh - avatar
4 Answers
+ 2
recursion could be a better option here. #include <iostream.h> int factorial(int); void main(void) { int number; cout << "Please enter a positive integer: "; cin >> number; if (number < 0) cout << "That is not a positive integer.\n"; else cout << number << " factorial is: " << factorial(number) << endl; } int factorial(int number) { int temp; if(number <= 1) return 1; temp = number * factorial(number - 1); return temp; }
4th Dec 2016, 5:39 PM
Shekhar Bahuguna
Shekhar Bahuguna - avatar
15th Oct 2017, 10:46 AM
David
David - avatar
17th Nov 2017, 9:39 AM
#RahulVerma
#RahulVerma - avatar
17th Nov 2017, 9:39 AM
#RahulVerma
#RahulVerma - avatar