C program to find factorial of a number
- Home
- Calculations
- C program to find factorial of a number
- On
- By
- 0 Comment
- Categories: Calculations
C program to find factorial of a number
C program to find factorial of a number
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
Code to find factorial of a number
Find factorial using for loop
#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
Find factorial using the while loop
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
Find factorial using the do-while loop
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
Find factorial using recursion
#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
Find factorial using the function
#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