Reputation: 105
Factorial program using recursion in C with while loop. Hi all thanks for your valuable replies.
You all said use (if condition Instead of while). Its correct I accept your suggestion. Then why don't I use while for finding the factorial using recursive function.
Somebody said while(n>1)
is an infinite loop. But in this program n
value is decremented in the fact(n-1)
function itself. Now in this program I put a printf()
and getch()
after the while loop to know the value of n
. The printf()
and getch()
function executes only when the while condition becomes false.
When I run this program the printf()
function and getch()
function executes repeatedly and the printf()
function return n
value = 1. So I determine that the value of n
is decremented. Then why does this program execute the while loop again and again?
Note: I am using Turbo C 3.0 to run this program,
#include<stdio.h>
int fact(int n)
{
int x=1;
while(n>1)
{
x=n*fact(n-1);
}
printf("N value after the while loop:%d",n);
getch();
return(x);
}
void main()
{
int n,fact1;
scanf("%d",&n);
fact1=fact(n);
printf("%d",fact1);
}
Upvotes: 0
Views: 4546
Reputation: 11
Here i would have a pretty easy code to understand for you. It is very short and effective. Of course i coded it using recursion:
The only header you need to include is stdio.h.
Main:
int main() {
unsigned long n;
scanf("%lu", &n);
printf("%lu\n", factorial(n));
}
Function calculation factorial:
unsigned long factorial(unsigned long n) {
if (n==1) {
return 1;
} else {
return n * factorial(n-1);
}
}
As you see it's a pretty short and effective program. I used unsigned longs, so that the program can output and can calculate with very long numbers without getting overflows or stuff like that. You don't need any kind of loop, just the recursive function.
Upvotes: 0
Reputation: 5456
You do have an infinite loop. The line fact(n-1)
doesn't decrease the value of n
. It invoked another call of the function with a smaller n
.
So, if you call fact(2)
, you have a call with n==2
. In this function, you have an infinite loop that call fact(1)
. In this second call, n==1
, so the loop condition is false, and this call prints your line and return - into the infinite loop of the first call (which its n
is still 2).
Upvotes: 2
Reputation: 3256
First of all may I suggest you put a prompt like that before scanf? It is strange to be prompted for a number by the console when there is no text asking you to do so. Looks like the program has hung.
printf("Give the value of n:");
So in order to fix your program I would suggest you do something like the example below. You have to understand how recursion works. You can't just be calculating a number inside the while(). You have to be returning something , else it's an infinite loop.
#include<stdio.h>
int fact(int n)
{
int x=1;
while(n>1)
{
x = n*fact(n-1);
return x;
}
return x;
}
void main()
{
int n,fact1;
printf("Please provide the value of \'n\':");
scanf("%d",&n);
fact1=fact(n);
printf("Result is %d",fact1);
return 0;
}
Upvotes: 1