forked from Romeo-Aryal/C-program-fest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial_using_recursion.c
More file actions
33 lines (27 loc) · 866 Bytes
/
factorial_using_recursion.c
File metadata and controls
33 lines (27 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
//Recursive Function to find the Factorial
int factorial(int number){
if (number==1 || number==0) //Terminating Condition of the Recursion
{
return 1;
}
else{
return (number * factorial (number-1)); //Function calling itself
}
}
//Main Function
int main()
{
int a;
printf("Enter a Number To Find The Factorial: ");
scanf("%d", &a); //Taking the number as user-input
if(a<0) //Condition if provided number is less than 0 (means, Factorial not possible!)
{
printf("Enter a valid number!");
}
else
{
printf("The Factorial of %d is %d", a, factorial(a)); //If the number is greater than or equal to 0, it calls the Factorial Function
}
return 0;
}