Reputation: 13
The program takes an input, and should calculate the factorial of the number. However, after the number has been inputted there is a delay and the program stops
so far I haven't changed the code much from my initial attempt as I do not fully understand recursion and sunbroutines in C.
int calcFactorial(int n);
int input = 0, answer = 0;
int main()
{
int n = 0;
printf("Enter number:\n");
scanf("%d", &input);
answer = calcFactorial(input);
printf("The factorial of %d is %d.\n", input, answer);
system("pause");
return 0;
}
int calcFactorial(int n){
int factorial = 0;
if (n==0){
factorial = 1;
}
else{
factorial = n * calcFactorial(n-1);
printf(factorial);
}
return factorial;
}
Upvotes: 0
Views: 1130
Reputation: 1371
However, after the number has been inputted there is a delay and the program stops
As has already been pointed out by others, printf(factorial)
has undefined behavior. It should be removed or it should be called properly (e.g. printf("%d\n", factorial)
).
Not sure why you have system("pause")
in there as it seems unnecessary.
Removing those two items from your code, I had no trouble running it and getting the correct answer for a factorial up to 12. Again, as has been pointed out by others, your function will only be valid up to 12!
due to integer overflow.
Other than that, your code could be formatted better for the input, for example:
/* formats better than original */
printf("Enter number: ");
scanf("%d", &input);
printf("\n");
You can slim down your calcFactorial
function as there is no need for the int factorial
variable or the else
clause. So you could make it look like this:
int calcFactorial(int n){
if (n == 0) return 1; //base case, 0! is 1
return n * calcFactorial(n - 1); //all other cases
}
I do not fully understand recursion and sunbroutines in C
Essentially, your function will be called over and over with n
, and each time it is called we refer to that as an instantiation, and in each instantiation n
is one less than it was in the previous instantiation. This will happen until the base case is reached (n == 0
) then that instance will return to the instance before it, and this will happen until all instances have returned.
For simplicity sake, let's walk through calcFactorial(3)
. The base case is reached which is the instance that terminates further recursive calls and returns 1
. In the instance before the base case was reached, n == 1
so that instance will return 1*1
since the instance before it returned 1
. The instance before that was n == 2
so that instance returns 2*1
. The instance before that was the first instance n == 3
, so that instance returns 3*2
. All instances have now returned and the result is 6 and that's what we expect for 3!
.
You can learn more about recursion here
Upvotes: 0
Reputation: 310980
This statement in the function calcFactorial
printf(factorial);
has undefined behaviour because the first parameter of the function printf
is declared as const char *
while you are supplying an object of the type int
.
Remove the statement from the function.
Or if you want to get intermediate values then write
printf( "%d\n", factorial);
Also take into account that for the type int
that usually has size of 4 bytes the maximum number for which you can get a valid value of the factorial is equal to 12
.
You could use type unsigned long long int
instead of the type int
. In this case you can calculate the factorial for a number equal to 20.
Here is a demonstrative program
#include <stdio.h>
unsigned long long int calcFactorial( unsigned long long int n )
{
return n == 0 ? 1 : n * calcFactorial( n - 1 );
}
int main( void )
{
unsigned long long int input = 0, answer = 0;
printf( "Enter number: " );
scanf( "%llu", &input);
answer = calcFactorial( input );
printf( "The factorial of %llu is %llu.\n", input, answer );
}
Its output might look like
Enter number: 20
The factorial of 20 is 2432902008176640000.
The return statement in the function can be rewritten also the following way
unsigned long long int calcFactorial( unsigned long long int n )
{
return n < 2 ? 1 : n * calcFactorial( n - 1 );
}
Upvotes: 3