C program to find the factorial using recurrsion
Input any number: 5
Factorial of 5 = 120
- First let us give a meaningful name to our function, say
fact()
. - The factorial function accepts an integer input whose factorial is to be calculated. Hence the function declaration should look like
fact(int num);
. - The function returns factorial as an integer value. Since, factorial can grow rapidly hence the suitable return type for this function is
unsigned long long
.
Source code:
Here we have the codes!!
#include<stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
No comments:
Post a Comment