Error to run program in Fortran

Asked

Viewed 177 times

0

I need to calculate the Minimum common splitter in Fortran, I did the code, but when it compiles and runs, it closes after the user enters 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

1 answer

0

Hello, although I could not understand well the objective of the program, since as far as I remember MDC calculation needs to have at least two values, here is a code with the corrections that I believe are applicable:

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 I believe are relevant are only two:

1) Variables must be integer, as you used them in the loop of the "DO"

2) Its "DO" loop started from scratch, which caused the "MOD" function to capsize. This was the reason for the crash that was happening.

If the answer is satisfactory, please do not forget to mark it to help other people who are researching something like it and also do not forget to evaluate positively. A hug.

  • Don’t forget to always indent your codes. This makes it easy to read and debug it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.