I wrote a C program to use recursive functions to find the factorial of a number and the code is given below:
//program to find factorial of a number using a recursive function
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int factorial(int a);
int main()
{
int num, fact;
printf("Enter a number: ");
scanf("%d", &num);
if(num<=0)
{
printf("The number cannot be zero/negative.");
exit(1);
}
fact=factorial(num);
printf("The factorial of the entered number is: %d", fact);
getch();
return 0;
}
int factorial(int a)
{
while(a>1)
return a*factorial(--a);
}
In the above code in 'while' loop if the input 'a'>1 then it should execute, but when i compile the program (using Dev C++ v.5.4.2) and give the input as 1, I get the factorial as 1. The program is the solution to the required problem, but i need to know what is actually happening in background...
Thank you in advance