2
votes
PROGRAM FactorPrimo(input,output);
VAR
    numero : integer;
    factor : integer;
    resto : integer;
    primo : integer;
    multiplicidad : boolean;

BEGIN
    write('Ingrese numero: ');
    readln(numero);
    factor := 2;
    primo := 0;
    multiplicidad := false;
    IF numero > 1 THEN
    BEGIN
        REPEAT
        BEGIN
            resto := numero MOD factor;
            IF resto = 0 THEN
            BEGIN
                numero := numero DIV factor;
                primo := primo + 1;
                multiplicidad := true;
            END
            ELSE IF multiplicidad = true THEN
                IF primo > 1 THEN
                writeln(factor,'^',primo);
                multiplicidad := false;
                ELSE
                writeln(factor);
                multiplicidad := false;
            ELSE
            factor := factor + 1;
            primo := 0;
            END;
        UNTIL numero = 1;
        writeln(factor);
    END
    ELSE 
        write('Debe ingresar un numero mayor a 1');
END.

I don't know why but the program doesn't enter in IF PRIMO > 1 then it always goes to the ELSE statment below. Have I done something wrong? I have done it in java and it works fine.

Working fine it should says:

factor^primo

But what displays is only:

factor

It should go from the if to the else if and the try the else. But what it is doing now is going from the if to the else and then else if.

An example, with the number 360 it should says:

2^3
3^2
5

But what it shows now is:

2
3
5

I think this is because it always end up with primo = 0, and I don't know how to fix this.

1
I have figuered it out, that it goes from the "if" statment to the "else" making primo always = 0. How can i fix this? - Javier Coronel
It jumps three times between the "if" and the "else" before going to the "else if". What have i done wrong? - Javier Coronel
Please include sample data and the output generated in your question along with a description of what you expect to see. - Richard Chambers
The program you have given is syntactically incorrect. It does not compile. - Stuart

1 Answers

0
votes

By changing the primo = 0 inside the else if did the trick, now works fine.

ELSE IF multiplicidad = true THEN
IF primo > 1 THEN
writeln(factor,'^',primo);
multiplicidad := false;
primo = 0;
ELSE
writeln(factor);
multiplicidad := false;
primo = 0;