Write a C program to calculate factorial of a number using recursion.
Program:-
#include <stdio.h>
mul(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, mul(n));
return 0;
}
mul(int n)
{
if (n >= 1)
return n*mul(n-1);
else
return 1;
}
Output:-
Program:-
#include <stdio.h>
mul(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, mul(n));
return 0;
}
mul(int n)
{
if (n >= 1)
return n*mul(n-1);
else
return 1;
}
Output:-
No comments:
Post a Comment