C program Factorial number
Factorial number - it is iterative multiplication from 1 to a given factorial number. The C program finds decimal value equivalent to given factorial number using iterative manner or for-loop.
Factorial number - example calculation
find decimal value for given factorial number F =6! F = 1 x 2 x 3 x 4 x 5 x 6 F = 24 x 5 x 6 F = 120 x 6 F = 720 decimal value 720 is equal to factorial 6!
Factorial - C programming code
The input,factorial number is read from user, while the program is running and calculate result for the factorial number using for-loop.
#include <stdio.h>
int main() {
int x=0;
printf("\n Find decimal value for given factorial number");
printf("\nEnter factorial number x :");
scanf("%d",&x);
int F=1;
for (int n=1; n<=x ; n++) {
F = F * n;
}
printf ("\n Result of Factorial");
printf("\n %d! = %d ",x,F);
return 0;
}
Factorial - C programming Output
Find decimal value for given factorial number Enter factorial number x 5 Result of Factorial 5! = 120
The C program finds decimal value equivalent to given factorial number using recursive function.
Factorial number using Recursive function - C programming code
The input value,factorial number is read from user and calculate a decimal value as result of the factorial number using recursive function.
#include <stdio.h>
int factorial(int f) {
if (f>0)
return f*factorial(f-1);
else
return 1;
}
int main()
{
int x=0,F=0;
printf("\n Find decimal value for given a factorial number");
printf("\nEnter factorial number x :");
scanf("%d",&x);
F=factorial(x);
printf ("\n Result of Factorial");
printf("\n %d! =: %d ",x,F);
return 0;
}
Output
Find decimal value for a given factorial number Enter factorial number x 4 Result of Factorial 4! = 24
Comments
Post a Comment