Labels

Saturday, December 5, 2015

To find the factorial of a number using Recursion in C;

To find the factorial of a number using Recursion in C;

#include <stdio.h>
int fact(int a);
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d",&n);
int a=fact(n);
return 0;
}
int fact(int a)
{
int result;
if(a==1)
result =1;
result= fact(a-1)*a;
return result;
}

Recursion in C























To compile this program in Linux:

  1. Save this file with extension .c
  2. g++ compiler come pre installed in linux. Type this in terminal g++ fact.c -o fact. Here, I have assumed that the file name is fact.c. -o fact will provide me with an output file with extension exe file and name fact.
  3. Now, run this file typing ./fact and press enter.
To compile this program using cmd:

  1. Save this file with extension.c
  2. Type this in the cmd g++ fact.c -o fact.
  3. To run type fact and press enter.
Note: To use g++ compiler in windows user has to install these compiler by itself as g++ compiler does not come per installed in windows operating system.

No comments:

Post a Comment