Factorial function does not return

0

I need to use function in C but it is not returning the factorial.

  

Problem: "Given an integer n, compute its factorial n !.   factorial of a number is given by the equation:

n! = n(n                    
asked by anonymous 20.09.2018 / 15:04

1 answer

2

The code is confusing, disorganized and has too many things. But the main problem is mixing concepts without discretion. The statement demonstrates what you have to do. The factorial is a number multiplied once followed by another by a sequence of numbers. This sequence ranges from 1 to the specified N. So the loop is very simple, goes from 1 to N, is in the statement. And the multiplication is the initial value of 1 (the basic unit) by that value being incremented. Just this.

There was also a formatting problem in printf() .

I suggest understanding the language mechanisms in a more basic way before using them. I mean, know each character of for and other things. Because you're putting everything together. Do not do anything without judgment, without being able to explain why you are doing it. Do not put a comma, a space without a justification. The same omit certain things.

#include <stdio.h>

unsigned long int fat(unsigned int n) {
    int fatorial = 1;
    for (int i = 1; i <= n; i++) fatorial *= i;
    return fatorial;
}

int main() {
    int numero;
    scanf("%d", &numero);
    printf("%d! = %ld", numero, fat(numero));
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
20.09.2018 / 15:13