/*
Write a program to find the factorial of a number by tail recursive method.
*/
#include<stdio.h>
long TailRecursiveFact(int n);
long TRfact(int n, int result);
main()
{
Write a program to find the factorial of a number by tail recursive method.
*/
#include<stdio.h>
long TailRecursiveFact(int n);
long TRfact(int n, int result);
main()
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
if(num<0)
printf("No factorial for negative number\n");
printf("Factorial of %d is %ld\n", num, TailRecursiveFact(num) );
}
/*Tail recursive*/
long TRfact(int n, int result)
return TRfact(n, 1);
}
printf("Enter a number : ");
scanf("%d", &num);
if(num<0)
printf("No factorial for negative number\n");
printf("Factorial of %d is %ld\n", num, TailRecursiveFact(num) );
}
/*Tail recursive*/
long TRfact(int n, int result)
{
if( n==0)
return result;
return TRfact(n-1, n*result);
}
/*Helper function*/
long TailRecursiveFact(int n)
{
if( n==0)
return result;
return TRfact(n-1, n*result);
}
/*Helper function*/
long TailRecursiveFact(int n)
{
return TRfact(n, 1);
}
0 Comments