Daniel Logvin
Daniel Logvin

Reputation: 502

Trying to know why is this code leading into an infinite loop

I have been trying to get the factorial of a number (f.ex: 4! = 24 [4 (3) (2) (1) = 24]).

And I wrote the following code:

#include <stdio.h>

void TestAcomulador(int a, int factorizado, int *resultado);

int main(void) {
    int a, resultado, factorizado, sacaroperacion;
    printf("Introduzca un numero: ");
    scanf("%d", &a);
    TestAcomulador(a, factorizado, &resultado);
    printf("%d", resultado);
}

void TestAcomulador(int a, int numprincipal, int *factorizado) {
    numprincipal = --a;
    do {
        *factorizado = numprincipal * a;
        printf("The loop is here\n"); //LOOP IN THIS LINE
    } while (numprincipal > 0);
}

What is going on and what I'm doing wrong? help would be appreciated to avoid getting this problems in the future,

thanks in advance.

Upvotes: 0

Views: 79

Answers (1)

Pranav
Pranav

Reputation: 686

You aren't decrementing numprincipal at any point, so it'll always be greater than 0 (assuming you a>=2).

So the while loop's condition is always true.

Upvotes: 2

Related Questions