In this tutorial, we will discuss the concept of C program to find factorial of a number
Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one
Example
factorial of 5 is
5!=5*4*3*2*1=120
factorial of 4 is
4!=4*3*2*1=24
The factorial of a positive number n is given below
factorial of n is
n!=n*(n-1)*....2*1
There are many ways to calculate a factorial using C programming language. some of them are described as below.
the user can provide numbers as they wish and get the factorial according to their input
#include <stdio.h> #include <stdlib.h> int main() { int n,i,factorial=1; printf("Enter an integer..: "); scanf("%d",&n); if(n<0){ printf("Factorial doesn't available to a negative number"); } else{ for(i=1; i<=n; i++){ factorial*=i; //this is equal to factorial=factorial*1 } printf("Factorial of %d is = %d ",n,factorial); } getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer..:8 Factorial of 8 is= 40320
case 2
Enter an integer..:0 Factorial of 0 is= 1
case 3
Enter an integer..:-3 Factorial doesn't available to a negative number
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int n,i,factorial=1; printf("Enter an integer..: "); scanf("%d",&n); if(n<0){ printf("Factorial doesn't available to a negative number"); } else{ i=1; while(i<=n){ factorial*=i; //this is equal to factorial=factorial*1 i++; } printf("Factorial of %d is = %d",n,factorial); } getch(); return 0; }
When the above code is executed, it produces the following results
Enter an integer..:5 Factorial of 5 is= 120
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int n,i,factorial=1; printf("Enter an integer..: "); scanf("%d",&n); if(n<0){ printf("Factorial doesn't available to a negative number"); } else{ i=1; do{ factorial*=i; //this is equal to factorial=factorial*1 i++; }while(i<=n); printf("Factorial of %d is = %d",n,factorial); } getch(); return 0; }
When the above code is executed, it produces the following results
Enter an integer..:6 Factorial of 6 is= 720
#include <stdio.h> #include <stdlib.h> long factorial(int); //function declaration int main() { int n; long fact; printf("enter the integer to find factorial\n"); scanf("%d", &n); if(n<0) printf("Factorial doesn't available to a negative number"); else{ fact=factorial(n);//call the method printf("%d! = %ld\n",n,fact); } getch(); return 0; } long factorial(int n) { if(n==0) return 1; else return(n*factorial(n-1)); }
When the above code is executed, it produces the following results
enter the integer to find factorial 6 6!=720
#include <stdio.h> #include <stdlib.h> long factorial(int); int main() { int num; long fact=1; printf("Enter a number to calculate factorial\n"); scanf("%d",&num); printf("%d! = %ld\n",num,factorial(num)); getch(); return 0; } long factorial(int n) { int i; long result=1; for(i=1; i<=n; i++) result=result*i; return result; }
When the above code is executed, it produces the following results
Enter a number to calculate factorial 6 6!=720
Suggested for you
C program for loop
C program do-while loop
C program recursion
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…