‘C’ to calculate factorial of a number using recursion.

C File =>


/* Write a recursive function in ‘C’ to calculate factorial of a number. Use 
this function in main. */

#include<stdio.h>
int main() 
{
      int num;

     // defining recursive function 
     int fact(int n)
    {
         if(n==1 || n==0)
                 return 1;
         else
                 return(n*fact(n-1));
     }

      printf("\n Enter number : ");
      scanf("%d",&num);
      printf("\n Factorial of %d is %d",num,fact(num));

     return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.