Factorial of a numebr n:
Factorial of a number n is the product of the numbers from 1 upto n. It is denoted by n!.
Example: Factorial of 3 is 3! = 1 \times 2 \times 3 = 6
Factorial of 5 is 5! = 1 \times 2 \times 3 \times 4 \times 5 = 120
Algorithm :
- Start the program
- Read the number n
- Initialize i = 1 and factorial =1
- Multiply factorial with i and store at factorial
- Increment the i
- Perform the step 4 and 5 until i = n
- Stop the program
#include"iostream" using namespace std; int main() { int num, factorial=1; cout<<" Enter a number to find the factorial : "; cin>>num; for(int i=1;i<=num; i++) { factorial=factorial*i; } cout<<"Factorial of the number is ="<<factorial << endl; return 0; }
INPUT : Enter the number to find the factorial : 8
OUTPUT : Factorial of the number is = 40320
No comments:
Post a Comment