C language practice programs
Factorial numbers are a mathematical concept that plays a crucial role in various branches of mathematics and computer science. The factorial of a non-negative integer is the product of all positive integers less than or equal to that number. It is denoted by the symbol !
.
For example:
- The factorial of 5, denoted as 5!, is calculated as 5×4×3×2×1=1205×4×3×2×1=120.
- The factorial of 0 is defined to be 1, denoted as 0!=10!=1.
Factorials often find applications in permutations, combinations, and probability, making them an essential concept in algorithm design and mathematical calculations.
Now, let’s explore how to calculate the factorial of a given number using the C programming language. Below is a simple C program that takes user input for a number and calculates its factorial:
Recursive function
#include <stdio.h>
// Function to calculate factorial recursively
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
// Input
printf("Enter a number: ");
scanf("%d", &num);
// Check for negative input
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate and display the factorial
printf("Factorial of %d = %d\n", num, factorial(num));
}
return 0;
}
This program defines a recursive function factorial
to calculate the factorial of a given number. The main
function takes user input, checks if it’s non-negative, and then calls the factorial
function to compute and display the result.
Iterative method:
#include <stdio.h>
// Function to calculate factorial iteratively
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int num;
// Input
printf("Enter a number: ");
scanf("%d", &num);
// Check for negative input
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate and display the factorial
printf("Factorial of %d = %d\n", num, factorial(num));
}
return 0;
}
In this program, the factorial
function uses a loop to calculate the factorial iteratively. The loop runs from 1 to the given number (n
), and the result is updated at each iteration. The rest of the program is similar to the previous one. It takes user input, checks for negative values, and then calculates and displays the factorial.
Remember, factorials can grow very quickly, and for large input values, the program may encounter integer overflow. In such cases, you may need to use a larger data type, such as long long
, to handle larger results.
Understanding factorial numbers and being able to calculate them is a fundamental skill in the world of mathematics and computer science. Whether you are working on algorithms, probability, or combinatorics, the concept of factorials will likely play a role in your problem-solving journey.
Read my other blogs:
Embedded C language Interview Questions.
Automotive Interview Questions
Understanding AUTOSAR Architecture: A Guide to Automotive Software Integration
Big Endian and Little Endian in Memory
Zero to Hero in C language Playlist
Embedded C Interview Questions
Subscribe my channel on Youtube: Yogin Savani