0% found this document useful (0 votes)
20 views21 pages

Factorial Programs in C

The document provides a step-by-step guide on how to calculate the factorial of a number using three methods in C: recursion, for loop, and while loop. It includes detailed code examples for each method, explaining the necessary steps such as defining the factorial function, getting user input, and printing the result. The document emphasizes the differences between using recursion and loops for calculating factorials.

Uploaded by

ARaMaa Deve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

Factorial Programs in C

The document provides a step-by-step guide on how to calculate the factorial of a number using three methods in C: recursion, for loop, and while loop. It includes detailed code examples for each method, explaining the necessary steps such as defining the factorial function, getting user input, and printing the result. The document emphasizes the differences between using recursion and loops for calculating factorials.

Uploaded by

ARaMaa Deve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Factorial Programs in C – A Step-by-Step Guide

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:

Using For Loop


Using Recursion
Using While Loop
Using Recursion
In this method, the factorial function calls itself repeatedly with the input value
decremented by 1, until the base case of 1 is reached. The result is then returned to the
previous call, and so on, until the final result is obtained. This method is also known as a
recursive factorial function.
Let’s look at a step-by-step guide for calculating the factorial of a number

1: Include the necessary libraries.

#include <stdio.h>

2: Define the factorial function.

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.

1: Include the necessary libraries 3: Initialize the result variable.


#include <stdio.h>
int factorial(int n) {
2: Define the factorial function. int result = 1;
// code to calculate factorial goes here
int factorial(int n) {
}
// code to calculate factorial goes here
}
4: Use a for loop to iterate through the numbers.

int factorial(int n) { To calculate the factorial, we will use a for loop


int result = 1; to iterate through the numbers from 1 to n. In
for (int i = 1; i <= n; i++) { each iteration, we will multiply the current
result = result * i; value of result by the loop variable, and store
} the result back in result. This will give us the
// code to return result goes here product of all the numbers from 1 to n, which
} is the factorial.
5: Return the result.
After the loop completes, we will return the
int factorial(int n) {
value of result as the final factorial value.
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
return result;
}
6: 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
}
7: 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
}
8: 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;
}
#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>

Using While Loop int factorial(int n) {


This method is similar to int result = 1;
int i = 1;
the for loop method, but
while(i <= n) {
uses a while loop instead. result = result * i;
A variable is initialized to i++;
1, and the while loop }
iterates until the input return result;
value is reached. In each }
iteration, the variable is
multiplied by the loop int main() {
int input;
variable, and the result is
printf("Enter a number: ");
stored in the variable. scanf("%d", &input);
int result = factorial(input);
printf("The factorial of %d is %d", input, result);
return 0;
}
Learn Data Types in C Programming With
Examples
https://www.shiksha.com/online-courses/articles/data-types-in-c-programming-with-
examples/

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

You might also like