Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

factorial int value in C

Tags:

c

factorial

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.

like image 899
Tom Benny Avatar asked Mar 28 '20 15:03

Tom Benny


People also ask

What is factorial value in C?

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.

Is there a factorial function in C?

Although there is no C function defined specifically for computing factorials, C math library lets you compute gamma function.

What is the factorial integer?

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.

What is the syntax of factorial?

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)!.

How do you find the factorial of a number in C?

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:

What is factorial in C program?

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.”

What is the factorial of 0 in C?

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.

What is the factorial of a number?

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 .


1 Answers

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.

like image 196
S.S. Anne Avatar answered Oct 19 '22 18:10

S.S. Anne