In this tutorial, we will learn to write the program to find the factorial of numbers using the C programming language. Here we are going to follow an iterative approach.
Please follow this article for factorial program in C using recursion
Before moving forward let’s understand
What is Factorial of a number?
Factorial of a whole number ‘n’ is the product of that number ‘n’ with its every whole number in descending order till 1.
For example
Factorial of 5 is 5*4*3*2*1 = 120
And factorial of 5 can be written as 5!.
Note: To store the largest output we are using long long data type. Long long takes double memory as compared to single long.
There are multiple ways to write the program in C to calculate the factorial of the whole number.
In this tutorial, we will learn to write using
1). Using For Loop
2). Using Function
1). Factorial Program in C of a given number using for Loop
#include<stdio.h>
int main() {
int i, num, factorial = 1;
printf("Enter a whole number to find Factorial = ");
scanf("%d", &num);
for (i = 1; i<=num; i++) {
factorial = factorial * i;
}
printf("Factorial of %d is: %d", num, factorial);
return 0;
}
Output
Program Explanations
- Header Inclusion and Main Function Declaration:
#include <stdio.h>
includes the Standard Input Output library necessary for input and output operations throughprintf
andscanf
.int main()
is the starting point for the program.
- Variable Declaration:
int i, num, factorial = 1;
declares three integer variables:i
is used as a loop counter.num
will store the user input number for which the factorial is to be calculated.factorial
is initialized to1
and is used to compute the factorial ofnum
.
- Input from User:
- The program prompts the user to enter a whole number (
num
). The input is read usingscanf
.
- The program prompts the user to enter a whole number (
- Calculation of Factorial:
- A
for
loop runs from1
tonum
. Within the loop,factorial
is updated by multiplying it withi
during each iteration. - This multiplication continues until
i
is equal tonum
, effectively calculatingnum!
(num factorial).
- A
- Output the Result:
- The factorial of the number is displayed using
printf
, which outputs the factorial stored infactorial
.
- The factorial of the number is displayed using
- Program Termination:
- The program returns
0
, indicating successful execution.
- The program returns
2). Factorial Program in C using Function and While Loop
#include<stdio.h>
int fact(int num) {
int i = 1, fact = 1;
while (i <= num) {
fact *= i;
i++;
}
return fact;
}
int main() {
int i, num, factorial = 1;
printf("Enter a whole number to find Factorial = ");
scanf("%d", & num);
printf("Factorial of %d is: %d", num, fact(num));
return 0;
}
Output
Program Explanations
- Header Inclusion:
#include <stdio.h>
: Includes the Standard Input Output library necessary for using theprintf
andscanf
functions for input and output operations.
- Function Definition for
fact
:int fact(int num)
: This function takes an integernum
as an argument and returns its factorial.- Within the function, two integer variables
i
andfact
are initialized to1
. The variablefact
will store the computed factorial. - A
while
loop iterates from1
tonum
. In each iteration,fact
is multiplied byi
, and theni
is incremented by1
. - Once the loop completes (when
i
exceedsnum
), the function returns the value offact
, which by then represents the factorial ofnum
.
- Main Function:
int main()
: The main entry point of the program.- Variables:
int i, num, factorial = 1
: Declares variables for loop counters and storage (factorial
is initialized but not actually used for computation inmain
).
- Input:
- The program prompts the user to enter a whole number using
printf
, and the input is read into the variablenum
usingscanf
.
- The program prompts the user to enter a whole number using
- Calculation and Output:
- The factorial of
num
is computed by calling thefact
function withnum
as an argument. - The result is then printed to the console using
printf
, displaying the factorial of the entered number.
- The factorial of
- Program Termination:
return 0;
: The main function ends and returns0
, indicating successful execution.