Factorial of a Number:- The factorial of a Number n, denoted by n!, is the product of all positive integers less than or equal to n.The value of 0! is 1 according to the convention for an empty product. For example:-
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
Write a program to find the Factorial of a number using 'While-Loop' :-
//Factorial of a number using 'While-Loop'
printf("Enter any number:");
printf("Factorial of a number is:%d",fact);
Enter any number:5
Factorial of a number is:120
1. First the computer reads the number to find the factorial of the number from the user.
2. Then using while loop the value of ‘i’ is multiplied with the value of ‘f’.
3. The loop continues till the value of ‘i’ is less than or equal to ‘n’.
4. Finally the factorial value of the given number is printed.
NOTE:- You can put this program inside some function if you want. And you can also design it in such a way that if the user inputs a number less than zero, you can ask him to enter the input again. This can be done by putting scanf statement inside a while loop and checking for the ‘not less than zero’ condition inside. And if condition holds we can break the loop to proceed further. You try it!