Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Factorial Calculation Using Recursion in C
Factorial Calculation Using Recursion in C
This C program calculates the factorial of a given positive integer using a recursive function. The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n.
✅ C Program Code:
#include <stdio.h>
int fact(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
int main()
{
int num;
printf("Enter the number that you want to find factorial:\n");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial number contain only positive:\n");
}
else
{
printf("Factorial of a given number %d is %d\n", num, fact(num));
}
}
๐ How It Works:
- fact(): This is the recursive function that calculates factorial. It returns 1 if n is 0 or 1 (base case). Otherwise, it multiplies n by the factorial of n-1.
- main(): Takes user input for the number and checks if it is negative. If negative, it prints an error message. Otherwise, it calls
fact()to calculate factorial and prints the result.
๐ป Sample Output:
5
Factorial of a given number 5 is 120
๐ Keywords:
factorial in C, recursive factorial program, factorial using recursion, C programming recursion example, factorial function in C, beginner C programs, recursion in C, factorial calculation
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment