Error to run program in fortran

0

I need to calculate the minimum common divisor in fortran, I did the code, but when it compiles and runs, it closes after the user types the value and I do not know what is wrong.

PROGRAM Calc
IMPLICIT NONE
REAL :: valor,contador=0, divisao
write(*,*) "Digite um valor positivo para calcular o MDC "
read(*,*) valor
do contador = 0, valor, 1 

if(MOD(valor,contador)==0)then 
divisao=divisao+1
endif
end do

if(divisao == 2) then
    write(*,*) "O Valor digitado possui MDC"
else
    write(*,*) "O valor digitado nao possui MDC"
endif

STOP
END PROGRAM Calc
    
asked by anonymous 01.12.2017 / 04:35

1 answer

0

Hello, although I have not been able to fully understand the purpose of the program, since as far as I remember MDC calculations need to have at least two values, here's a code with the corrections that I think are appropriate:

PROGRAM CALC
  IMPLICIT NONE
  INTEGER*4 :: valor,contador,divisao
  WRITE(*,*) "Digite um valor inteiro positivo para calcular o MDC "
  READ(*,*)  valor
  DO contador = 1, valor, 1
    IF (MOD(valor,contador).EQ.0) THEN
      divisao=divisao+1
    END IF
  END DO
  IF (divisao .EQ. 2) THEN
    WRITE(*,*) "O Valor digitado possui MDC"
  ELSE
    WRITE(*,*) "O valor digitado nao possui MDC"
  END IF
  READ(*,*)
  STOP
END PROGRAM CALC

The changes that I believe to be relevant are just two:

1) Variables must be integers, since you used them in the "DO" command loop

2) Your "DO" loop started from scratch, which caused the "MOD" function to flip. That was the reason for the crash.

If the answer is satisfactory, please do not forget to mark it to help others who are searching for something similar and also do not forget to evaluate positively. A hug.

    
07.02.2018 / 17:46