Factorial Programs in C
Factorial Programs in C
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in java language. Let's see the 2
ways to write the factorial program in java.
1) Factorial Program using loop
2) Factorial Program using recursion
A factorial is a mathematical operation that returns the product of all positive integers
less than or equal to a given positive integer. The symbol for factorial is “!”, and the
notation for the factorial of a number n is “n!”. For example, the factorial of 5 is 5! = 5 x
4 x 3 x 2 x 1 = 120. In this article, we will discuss how to calculate factorial using three
straightforward methods. We will be covering the following sections today:
#include <stdio.h>
int factorial(int n) {
// code to calculate factorial goes here
}
3: Define the base case.
In the factorial function, we will define the base case which is the stopping point of
the recursion. The base case is defined as the point where the recursion stops. In this
case, when the input value is 1, the function will return 1, which is the factorial of 1.
int factorial(int n) {
if (n == 1) {
return 1;
}
// code to calculate factorial goes here
}
4: Define the recursive case.
In the recursive case, we will call the factorial function again, but this time with the input
value decremented by 1. The result of this call will be multiplied by the current input
value and stored in a variable.
int factorial(int n) {
if (n == 1) {
return 1;
}
int result = factorial(n-1) * n;
return result;
}
5: Get input from the user.
In the main function, we will declare a variable to hold the input value, and use
the scanf() function to read the value from the user.
int main() {
int input;
printf("Enter a number: ");
scanf("%d", &input);
// code to call factorial function goes here
}
6: Call the factorial function.
We will pass the input value to the factorial() function and store the result in a
variable.
int main() {
int input;
printf("Enter a number: ");
scanf("%d", &input);
int result = factorial(input);
// code to print result goes here
}
7: Print the result to the user.
Finally, we will use the printf() function to print the final result to the user.
int main() {
int input;
printf("Enter a number: ");
scanf("%d", &input);
int result = factorial(input);
printf("The factorial of %d is %d", input,
result);
return 0;
}
The complete code for the program:
#include <stdio.h>
int factorial(int n) { Output:
if (n == 1) {
Enter a number: 5
return 1;
} The factorial of 5 is 120
int result = factorial(n-1) * n;
return result;
}
int main() {
int input;
printf("Enter a number: ");
scanf("%d", &input);
int result = factorial(input);
printf("The factorial of %d is %d", input, result);
return 0;
} It is important to note that recursion can take
up more memory than a loop,
Using For Loop
This is the most basic method for calculating a number's factorial. A for loop
iterates through the numbers from 1 to the given number, and the product
of all the numbers is calculated and stored in a variable. This variable is then
returned as the final factorial value.
int main() {
int input;
printf("Enter a number: ");
scanf("%d", &input);
int result = factorial(input);
// code to print result
}
8: Print the result to the user.
int main() {
int input;
printf("Enter a number: ");
scanf("%d", &input);
int result = factorial(input);
printf("The factorial of %d is %d", input,
result);
return 0;
}
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
} Output:
return result;
}
int main() {
int input;
printf("Enter a number: ");
scanf("%d", &input);
int result = factorial(input);
printf("The factorial of %d is %d", input, result);
return 0;
}
#include <stdio.h>
https://www.shiksha.com/online-courses/
articles/if-else-statement-in-c/
https://www.shiksha.com/online-courses/
articles/difference-between-while-and-do-
while-loop/ https://www.javatpoint.com/factorial-
program-in-java