Problem Statement: factorial of any number is equal to multiplication of all the numbers from 1 to that number. Factorial of 5 will be equal to multiplication of numbers from 1 to 5 which will be 1*2*3*4*5 = 120
Recursive Technique: If a function calls itself then it is called recursive function.
#include<stdio.h>
//recursive function for calculating factorial
int fact(int i)
{
int k=1;
if(i<=1)
{
return 1;
}
else
{
k=i*fact(i-1);
}
return k;
}
void main()
{
int n,factorial=1;
printf("Enter a no");
scanf("%d",&n);
factorial=fact(n);
printf("Factorial = %d",factorial);
}
output
Enter a no 5
Factorial = 120
Recursive Technique: If a function calls itself then it is called recursive function.
#include<stdio.h>
//recursive function for calculating factorial
int fact(int i)
{
int k=1;
if(i<=1)
{
return 1;
}
else
{
k=i*fact(i-1);
}
return k;
}
void main()
{
int n,factorial=1;
printf("Enter a no");
scanf("%d",&n);
factorial=fact(n);
printf("Factorial = %d",factorial);
}
output
Enter a no 5
Factorial = 120
No comments:
Post a Comment