-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_iterative_factorial.c
73 lines (56 loc) · 2.1 KB
/
ft_iterative_factorial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
int ft_iterative_factorial(int nb)
{
int result;
result = 1;
if (nb < 0)
return (0);
while (nb > 0)
{
result *= nb;
nb--;
}
return (result);
}
_______________________________________________________________________________________________
Explanation
Function Declaration:
int ft_iterative_factorial(int nb):
This function calculates the factorial of a given number nb iteratively. It returns the result as an integer.
Variable Initialization:
int result;:
This variable is initialized to 1 and will hold the intermediate and final results of the factorial computation.
Handling Negative Input:
if (nb < 0)
return (0);
If nb is negative, the function returns 0, because the factorial of a negative number is undefined.
Iterative Factorial Calculation:
while (nb > 0):
This loop continues as long as nb is greater than 0. Inside the loop:
result *= nb;
This line multiplies result by nb, updating the current factorial value.
nb--;
This line decreases the value of nb by 1, moving the calculation closer to completion.
Return Statement:
return (result);
Once the loop completes, the function returns the calculated factorial value stored in result.
Português:
Declaração da Função:
int ft_iterative_factorial(int nb):
Esta função calcula o fatorial de um número dado nb de forma iterativa. Ela retorna o resultado como um inteiro.
Inicialização de Variáveis:
int result;:
Esta variável é inicializada como 1 e armazenará os resultados intermediários e o resultado final do cálculo do fatorial.
Tratamento de Entrada Negativa:
if (nb < 0)
return (0);
Se nb for negativo, a função retorna 0, pois o fatorial de um número negativo é indefinido.
Cálculo Iterativo do Fatorial:
while (nb > 0):
Este loop continua enquanto nb for maior que 0. Dentro do loop:
result *= nb;
Esta linha multiplica result por nb, atualizando o valor fatorial atual.
nb--;
Esta linha diminui o valor de nb em 1, movendo o cálculo para mais perto da conclusão.
Instrução de Retorno:
return (result);
Quando o loop for concluído, a função retorna o valor fatorial calculado armazenado em result.