Table of Contents
In this tutorial, we will discuss the C program to find factorial of a number|in 7 ways
In this post, we are going to learn how to find the factorial of a number or the given number in C language
What is the factorial of a number (n)?
The factorial of a number (n) is the product of all positive integers from 1 up to n (n is the given number).
It is simply denoted by n!.
Example
if you want to find the factorial of number 5, you can follow this method.
Factorial of 5 will be 5*4*3*2*1=120
So, 5!=120.
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,num=5,fact=1;
for(i=1; i<=num; i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",num,fact);
getch();
return 0;
} When the above code is executed it produces the following output
Factorial of 5 is: 120
In this program
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,num,fact=1;
printf("Enter the number for find factorial: ");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",num,fact);
getch();
return 0;
}
When the above code is executed it produces the following output
Enter the number for find factorial: 4 Factorial of 4 is: 24
The program allows the user to enter a value and it finds and displays factorial of the given number using for loop in C language
Program 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,fact=1;
printf("Enter the number for find factorial\n");
scanf("%d",&num);
int i=1;
while(i<=num){
fact=fact*i;
i++;
}
printf("Factorial of %d is : %d",num,fact);
getch();
return 0;
}
When the above code is executed it produces the following output
Enter the number for find factorial 7 factorial of 7 is:5080
The program allows the user to enter a value and it finds and displays factorial of the given number using while loop in C language
Program 4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,fact=1;
printf("Enter the number to find factorial\n");
scanf("%d",&num);
int i=1;
do{
fact=fact*i;
i++;
} while(i<=num);
printf("Factorial of %d is : %d",num,fact);
getch();
return 0;
}
When the above code is executed it produces the following output
Enter the number to find fatorial 4 Factorial of 4 is : 24
The program allows the user to enter a value and it finds and displays factorial of the given number using the do-while loop in C language
Program 5
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter the number for find factorial\n");
fact(); //function call
}
int fact(){ //function definition
int i,num,fact=1;
scanf("%d",&num);
i=1;
while(i<=num){
fact=fact*i;
i++;
}
printf("Factorial of %d is : %d",num,fact);
//Display factorial
getch();
return 0;
}
When the above code is executed it produces the following output
Enter the number for find factorial 6 factorial of 6 is: 720
The program allows the user to enter a value and it finds and displays factorial of the given number using the function in C language
Program 6
#include <stdio.h>
#include <stdlib.h>
long int findFact(int num);
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d",&num);
printf("factorial of %d = %ld",num,findFact(num));
getch();
return 0;
}
long int findFact(int num){
if (num>=1)
return num*findFact(num-1);
else
return 1;
}
When the above code is executed it produces the following output
Enter a positive integer: 8 factorial of 8 = 40320
The program allows the user to enter a value and it finds and displays factorial of the given number using recursion in C language
Program 7
#include <stdio.h>
#include <stdlib.h>
void Factorial(int, long int*);
int main()
{
long int fact;
int num;
printf("Enter a integer to find factorial\n");
scanf("%d",&num);
Factorial(num,&fact);
printf("factorial of %d is %d ",num,fact);
getch();
return 0;
}
void Factorial(int n, long int *fact){
int i;
*fact=1;
for(i=1; i<=n; i++)
*fact=*fact*i;
}
When the above code is executed it produces the following output
Enter a integerto find factorial 5 factorial of 5 is 120
The program allows the user to enter a value and it finds and displays factorial of the given number using the pointer in C language
Similar post
C program to find factorial of a number
Java program to find factorial of a number
Python program to find factorial of a number
C++ program to find factorial of a number
Suggested for you
for loop in C language
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.