D
drM
I have looked at the faq and queried the archives, but cannot seem to
be able to get this to work. It's the usual factorial recursive
function, but that is not the problem. It hangs after the user enters a
number. However, as I indicate, if one adds something else after the
number, the function proceeds and finishes successfully.
I would appreciate some helpful hints.
thanks in advance.
#include <stdio.h>
#include <math.h>
int factorial( int);
main() {
int i, k;
printf ( "Please enter a number "); // <<<<< seems to hang here if
user enters number only, but
// works if user enters number eg 8 plus something else eg the letter
"m".
scanf (" %d ", &i);
k = factorial (i);
printf ( " the factorial of %d is %d\n ", i, k);
return (0);
}
int factorial ( int j){
if (j == 1)
return 1;
return j * factorial(j-1);
}
be able to get this to work. It's the usual factorial recursive
function, but that is not the problem. It hangs after the user enters a
number. However, as I indicate, if one adds something else after the
number, the function proceeds and finishes successfully.
I would appreciate some helpful hints.
thanks in advance.
#include <stdio.h>
#include <math.h>
int factorial( int);
main() {
int i, k;
printf ( "Please enter a number "); // <<<<< seems to hang here if
user enters number only, but
// works if user enters number eg 8 plus something else eg the letter
"m".
scanf (" %d ", &i);
k = factorial (i);
printf ( " the factorial of %d is %d\n ", i, k);
return (0);
}
int factorial ( int j){
if (j == 1)
return 1;
return j * factorial(j-1);
}