C Program to Find Factorial Using Function with Recursion and Without Recursion
/*
Write a Program using function to Find out Factorial of given number with and without recursion.
*/
#include <stdio.h>
int fact_rec(int); \\ With Recursion
int fact(int); \\Without Recursion
int main(void)
{
int n;
printf(" Enter Value n = ");
scanf("%d",&n);
printf("\n Without Rec = %d",fact(n));
printf("\n With Rec = %d",fact(n));
return 0;
}
int fact(int n) \\Without Recursion
{
int ans = 1;
while(n>=1)
{
ans = ans * n;
n--;
}
return ans;
}
int fact_rec(int n) \\ With Recursion
{
int ans = 1;
if(n==1)
{
return ans;
}
ans = ans * fact_rec(n-1);
}
Write a Program using function to Find out Factorial of given number with and without recursion.
*/
#include <stdio.h>
int fact_rec(int); \\ With Recursion
int fact(int); \\Without Recursion
int main(void)
{
int n;
printf(" Enter Value n = ");
scanf("%d",&n);
printf("\n Without Rec = %d",fact(n));
printf("\n With Rec = %d",fact(n));
return 0;
}
int fact(int n) \\Without Recursion
{
int ans = 1;
while(n>=1)
{
ans = ans * n;
n--;
}
return ans;
}
int fact_rec(int n) \\ With Recursion
{
int ans = 1;
if(n==1)
{
return ans;
}
ans = ans * fact_rec(n-1);
}
Comments
Post a Comment