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
Similar post
Java program to find factorial of a number
C++ program to find factorial of a number
C program to find factorial of a number
Python program to find factorial of a number
Java program to find factorial of a number using method
C++ program to find factorial of a number using function
C program to find factorial of a number using function
Python program to find factorial of a number using function
Java program to find factorial of a number using recursion
C++ program to find factorial of a number using recursion
C program to find factorial of a number using recursion
Python program to find factorial of a number using recursion
Suggested for you
Array Data Structure: Definition, Types, Operations & Advantages Array Data Structure Introduction In this post,…
20 ways to subtract two numbers in Java In this article, we will discuss the…
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…
Python: Average of Odd & Even Numbers from User Input In this post, we will…