Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C programs to calculate factorial of a no.by using both recursive and non-recursive functions


#include<stdio.h>
#include<conio.h>

unsigned int recr_factorial(int n);
unsigned int iter_factorial(int n);

void main()
{
  int n,i;
  long fact;
  clrscr();
  printf("Enter the number: ");
  scanf("%d",&n);

  if(n==0)
    printf("Factorial of 0 is 1\n");
  else
  {
    printf("Factorial of %d Using Recursive Function is %d\n",n,recr_factorial(n));
    printf("Factorial of %d Using Non-Recursive Function is %d\n",n,iter_factorial(n));
   }
   getch();
}

/* Recursive Function*/
unsigned int recr_factorial(int n) {
    return n>=1 ? n * recr_factorial(n-1) : 1;
}

/* Non-Recursive Function*/
unsigned int iter_factorial(int n) {
    int accu = 1;
    int i;
    for(i = 1; i <= n; i++) {
accu *= i;
    }
    return accu;
}



This post first appeared on C Programs, please read the originial post: here

Share the post

C programs to calculate factorial of a no.by using both recursive and non-recursive functions

×

Subscribe to C Programs

Get updates delivered right to your inbox!

Thank you for your subscription

×