What is Factorial?
The Factorial of a specified number refers to the product of a given series of consecutive whole numbers beginning with 1 and ending with the specified number
We use the “!” to represent factorial
Eg.
5! = 1 x 2 x 3 x 4 x 5 = 120
It’s A Fact!
The number of ways of arranging n objects is n!
n! = n x (n − 1) x (n − 2) x . . . x 3 x 2 x 1
To Find Factorial Of A Number Using C Program
Factorial program in c using for loop
/** Factorial program in c using for loop by codebind.com */ #include<stdio.h> int main(){ int i,f=1, num; printf("Enter a natural number: "); scanf("%d", &num); for(i=1;i<=num;i++) f = f * i; printf("%d! = %d\n", num, f); return 0; } /* OUTPUT: Enter a natural number: 5 5! = 120 */
How do you implement the factorial function in C
/** Factorial program in c using Using function by codebind.com */ #include<stdio.h> int Factorial(int num){ int i,f=1; for(i=1;i<=num;i++) f=f*i; return f; } int main(){ int i,factorial,num; printf("Enter a natural number: "); scanf("%d",&num); factorial = Factorial(num); printf("%d! = %d\n", num, factorial); return 0; } /* OUTPUT: Enter a natural number: 5 5! = 120 */
Write A C program to print Factorial series in a given range
/** Write A C program to print Factorial series in a given range by codebind.com */ #include<stdio.h> int main(){ long f=1; int i,num,min,max; printf("Enter the minimum range: "); scanf("%d",&min); printf("Enter the maximum range: "); scanf("%d",&max); printf("Factorial series in given range: "); for(num=min;num<=max;num++){ f=1; for(i=1;i<=num;i++) f=f*i; printf("%ld ",f); } return 0; } /* OUTPUT: Enter the minimum range: 1 Enter the maximum range: 10 Factorial series in given range: 1 2 6 24 120 720 5040 40320 362880 3628800 */
Write a C program to find Factorial using recursion
/** Write A C program to find Factorial using recursion by codebind.com */ #include<stdio.h> int Factorial(int i){ int result = 1; while (i > 0) { result = result * i; i = i-1; } return(result); } int main(){ int i,factorial,num; printf("Enter a natural number: "); scanf("%d",&num); factorial = Factorial(num); printf("%d! = %d\n", num, factorial); return 0; } /* OUTPUT: Enter a natural number: 5 5! = 120 */
Leave a Reply