Factorial value of given number
The factorial of n (n!) is nothing but n * n - 1 * n - 2 * .... * 2 * 1.
If the given number is 5 then the output should be factorial of 5 i.e, 5!=5*4*3*2*1=120
INPUT: 5
OUTPUT:120
INPUT: 5
OUTPUT:120
If the given number is 3 then the output should be 3! that is 3!=3*2*1=6
INPUT: 3
OUTPUT: 6
Program:
/*C program to find factorial of given number*/
/*C program to find factorial of given number*/
| #include<stdio.h> |
#include<conio.h>
int main()
{
int n,i,fact = 1;
clrscr();
printf("\n Enter a number: ");
scanf("%d ", &n);
i = n;
if(i == 0)
{
printf("\n Factorial of given number is: %d",fact);
}
}
else
{
do
{
fact = fact * i;
i = -i;
}while(i >= 1);
printf('\n The factorial of given number is : %d', fact);
}
return 0;
}
OUTPUT:
| Enter a number: 4 The factorial of given number is : 24 |
Program explanation:
1. We will include the header files in the given program, the included header files are conio.h and stdio.h
2. clrscr() will clear the display screen
3. printf() is used to display the given text on the screen.
4. scanf() is used to take the given input.
5. Variable n is used to store the given input.
6. The value of n is stored in the variable i.
7. If the condition mentioned in if() is true then if() will get executed.
8. If the condition is false then else block will get executed.
9. do while is an exit control loop which is used to find the factorial value of the given number.
10. return() will return the value to the main() funtion.
No comments:
Post a Comment