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;
}
#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:
- Save this file with extension .c
- 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.
- Now, run this file typing ./fact and press enter.
- Save this file with extension.c
- Type this in the cmd g++ fact.c -o fact.
- To run type fact and press enter.
No comments:
Post a Comment