The blog offers an in-depth exploration of all the methods with which we can write factorial programs in C. Further, this blog covers fundamental concepts, algorithms, pseudocode, and various approaches to writing factorial programs, utilizing loops and recursion.
Table of Contents
What is Factorial?
A factorial is a mathematical function denoted by an exclamation mark (!) that is used on the positive integers. The factorial of a positive integer n is the product of all unique positive integers less than or equal to n. In general, the factorial of a non-negative integer n is calculated as n! = n× n-1 × n-2 × …. × 2 × 1. For example, 5 factorial (5!) is 5 × 4 × 3 × 2 × 1, which equals 120. It is commonly used in math for counting permutations and combinations.
Concepts to Write a C Factorial Program
To write a C factorial program, we’ll need to understand these concepts:
- C Data Types: These are classifications that define the type of data a variable holds, like integers (int), which store whole numbers, and long integers (long int) for larger whole numbers.
- C Programming Operators: These symbols perform operations on variables. For instance, the multiplication operator (*) is used to multiply numbers in a factorial calculation.
- if…else Statement: This is a conditional statement that allows the program to make decisions based on certain conditions. It evaluates whether a condition is true or false and performs actions accordingly.
Example of an if…else statement:
In this example, the program checks if the variable num is greater than zero. If the condition is true, it prints “Number is positive.” If false, it prints “Number is not positive.
- C ‘for’ Loop: The for loop is used for iterative execution. It consists of an initialization, condition check, and increment/decrement expression to execute a block of code repeatedly until the condition isn’t true anymore.
Example of a for loop:
This for loop iterates from i = 1 to i = 5. Inside the loop, it prints the value of i (from 1 to 5) and then adds a space. After the loop ends, it prints a newline. The output will be 1 2 3 4 5.
- C ‘while’ Loop: The while loop is another looping structure that repeats a block of code as long as the specified condition is true. It continues execution as long as the condition remains true.
Example of a while loop in C:
In this example, the while loop starts with i = 1. It continues to execute the block of code inside the loop as long as the condition i <= 5 remains true. Inside the loop, it prints the value of i and increments i by 1 (i++). The loop runs until i becomes 6, at which point the condition becomes false and the loop terminates. The output will be 1 2 3 4 5.
Learn the fundamentals and advanced concepts of C with C Programming Certification Course!.
Get 100% Hike!
Master Most in Demand Skills Now!
Algorithm for Factorial Program in C
This algorithm outlines the steps to calculate the factorial of a number in C by iterating from 1 up to the given number and continuously multiplying the numbers to find the factorial.
- Start
- Declare variables ‘num’, factorial, and ‘i’ as integers
- Input an integer num from the user to find its factorial
- Set factorial to 1
- Loop from i = 1 to num:
- Multiply ‘factorial’ by i and store the result back in factorial
- End of loop
- The value of ‘factorial’ at the end of the loop is the factorial of the entered number num
- End
Pseudocode of Factorial Program
Utilizing the algorithm mentioned earlier, we can generate pseudocode for a C program that calculates the factorial of a given number, like this:
INPUT: n (non-negative integer)
OUTPUT: factorial of n(n!)
Initialize factorial = 1
For i = 1 to n:
factorial = factorial * i
Print factorial
Check out C and Data Structure Interview Questions to crack your next interview!
C Program to Find Factorial
There are two methods with which we can write a program for finding factorials in C:
Factorial in C Using Loops
- Using ‘for’ Loop
This method utilizes a for loop to calculate the factorial of a given number. The loop starts at 1 and iterates up to the given number, multiplying each integer in the range to calculate the factorial. For example, to find the factorial of 5 (denoted as 5!), the loop multiplies 1 * 2 * 3 * 4 * 5, which results in the factorial of 120.
- Using ‘while’ Loop
This code asks the user to enter a positive integer. It then calculates the factorial using a while loop by continuously multiplying the current factorial value with the number (num) and decreasing num by 1 in each iteration until num becomes 0. The result is displayed as the factorial of the entered number. If a negative number is entered, it shows an error message since the factorial of negative numbers is undefined.
Factorial in C Using Recursion
Recursion is a technique where a function calls itself until a base condition is met. In this method, a function called factorial is created. This function takes an integer argument and recursively calls itself until the base condition is reached. The base condition checks if the given number is 0 or 1, in which case the function returns 1. For any other positive number ‘n’, the function recursively calls factorial (n-1) and multiplies it by ‘n’ to find the factorial. For instance, to calculate 5!, the function calculates 5 * 4 * 3 * 2 * 1, which results in the factorial of 120.
Learn the usage of jump statements in C with the help of our blog on Break and Continue statements in C.
Function for Finding Factorial in C
This program calculates the factorial of a non-negative integer using recursion.
- The function factorial takes an unsigned integer n as its argument and returns an unsigned integer.
- Inside the function, it checks if n is 0 or 1, in which case the factorial is 1, and the function returns 1.
- If n is greater than 1, the function calls itself recursively with the argument n – 1, multiplying n with the result of the factorial of (n – 1).
- The main function prompts the user to enter a non-negative integer, calls the factorial function with the entered number, and prints the result.
Wrapping Up
Factorials are not just for math; they are used in many real-life situations. Businesses use them to make better decisions and organize things better. They help computer programs run faster and smarter. Factorials are also important in creating games and puzzles, and in keeping information safe in cryptography. In science, they help understand particles in quantum physics and are used in studying genes and DNA. They also help in planning and organizing in industries, and in predicting risks in finance and economics. So, factorials are really useful in many different areas, not just in math problems.