so i've been recently doing a homeschool project in C. We were asked to make code which returns you the factorial of the number you put in. That's cool and simple, but we were also asked to make something, what will write you an error, if the number of factorial is higher value than the max value of integer.
int main() {
int a, i;
int faktorial = 1;
a = 10;
if (a < 0)
printf("Chyba, faktorial z nekladneho cisla neexistuje. \n");
else {
for (i = 1; i <= a; ++i) {
faktorial *= i;
}
printf("Faktorial z %d = %d\n", a, faktorial);
}
return 0;
}
That's my code, which works perfectly, unless the value of factorial is bigger than the value of int. I've tried to make if statement, like if (faktorial > 2147483647) than printf error message, but it just didnt work and it still makes faktorial = 0.
Factorial of a positive integer (number) is the sum of multiplication of all the integers smaller than that positive integer. For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 which equals to 120. Factorial Program in C: All positive descending integers are added together to determine the factor of n.
Although there is no C function defined specifically for computing factorials, C math library lets you compute gamma function.
factorial, in mathematics, the product of all positive integers less than or equal to a given positive integer and denoted by that integer and an exclamation point. Thus, factorial seven is written 7!, meaning 1 × 2 × 3 × 4 × 5 × 6 × 7. Factorial zero is defined as equal to 1.
The factorial of n is denoted by n! and calculated by multiplying the integer numbers from 1 to n. The formula for n factorial is n! = n × (n - 1)!.
C Program to Find Factorial of a Number. The factorial of a positive integer n is equal to 1*2*3*...n. You will learn to calculate the factorial of a number using for loop in this example. To understand this example, you should have the knowledge of following C programming topics: The factorial of a positive number n is given by:
The following article, Factorial in C Program, provides an outline for C’s topmost factorial methods. The symbol for factorial is denoted by using this! ‘ sign. For instance, the number 6 factorial is referred to as 6!. Number factorial is described as the product “of the number, and all the entries are smaller than zero and negative.”
C Programming Operators. C if...else Statement. C for Loop. The factorial of a positive number n is given by: factorial of n (n!) = 1 * 2 * 3 * 4....n. The factorial of a negative number doesn't exist. And, the factorial of 0 is 1.
Since the factorial of a number may be very large, the type of factorial variable is declared as unsigned long long . If the user enters a negative number, the program displays a custom error message. You can also find the factorial of a number using recursion .
You can't test if a factorial is greater than INT_MAX
with >
because an int
will never be greater than INT_MAX
. Instead, you can divide INT_MAX
by a
beforehand, and check if faktorial
is ever greater than that. This is so you don't have to divide on every iteration:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* ... */
int bound = INT_MAX / a;
for (i = 1; i <= a; ++i) {
if(faktorial > bound)
{
fputs("Integer Overflow!\n", stderr);
return EXIT_FAILURE;
}
faktorial *= i;
}
Assuming a
is strictly positive, this will always work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With