In: Computer Science
Write a C function to calculate and return the factorial value
of any positive integer as an
argument. Then call this function from main() and print the results
for the following input
values:
2, 3,4, 5, 10, 15
What is the maximum value of an integer for which factorial can be
calculated correctly on a
machine using your algorithm?
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
long long factorial(int num);
int findMaxValue();
int main() {
int num;
printf("Enter a number :");
scanf("%d",&num);
printf("Factorial of %d is %lld\n",num,factorial(num));
printf("Maximum value of an integer for which factorial can be
calculated correctly is %d\n",findMaxValue());
return 0;
}
long long factorial(int num) {
if (num <= 1)
return 1;
else
return num * factorial(num - 1);
}
int findMaxValue()
{
int res = 2;
long long int fact = 2;
while (1)
{
if (fact < 0)
break;
res++;
fact = fact * res;
}
return res - 1;
}
___________________________
Output:
__________________Thank You