Fortran: Missing ")" in statement

I have a problem with my Fortran program, which does nothing more than calculate simple factorization (or should do). What a mistake:

C:\MinGW\Fortran>gfortran aufg3.f90 aufg3.f90:15.15: if (prim(i) != 0 .and. modulo(n, prim(i)) == 0) then 1 Error: Missing ')' in statement at or before (1) aufg3.f90:19.7: end if 1 Error: Expecting END DO statement at (1) aufg3.f90:34.13: if (prim(i) != 0) then 1 Error: Missing ')' in statement at or before (1) aufg3.f90:38.5: end if 1 Error: Expecting END DO statement at (1) 

I tried everything, but I don’t know at all what could be wrong. Thank you for your help. Here is the code:

 program aufg3 implicit none integer :: n, i integer, allocatable, dimension(:) :: prim do print *, 'Bitte natürliche Zahl eingeben, "0" für Abbruch: ' read *, n if (n == 0) exit allocate(prim(2:n)) call era(prim, n) print *, n, ' = 1' do if (n == 1) exit do i = 2, n if (prim(i) != 0 .and. modulo(n, prim(i)) == 0) then print *, ' * ', prim(i) n = n / prim(i) exit end if end do end do deallocate(prim) end do end program 

subroutine era(prim, m) integer, intent(in) :: m integer, dimension(2:m) :: prim integer :: i, j do i = 2, m prim(i) = i end do do i = 2, integer(sqrt(real(m))) if (prim(i) != 0) then do j = i*i, m, i prim(j) = 0 end do end if end do end subroutine

+5
source share
3 answers

Well, this is Fortran, ah ! indicates a comment. Therefore, the compiler really sees

 if (prim(i) 

which is not valid. The error message you see reflects this.

Uneven in Fortran - /= or .ne. :

  if (prim(i) /= 0 .and. modulo(n, prim(i)) == 0) then 

and then:

 if (prim(i) /= 0) then 
+6
source

You are using the wrong notation for not equal. Syntax Fortran /= or .NE. .

So you should use:

 if (prim(i) /= 0 .and. modulo(n, prim(i)) == 0) then 

and

 if (prim(i) /= 0) then 

Also, your integer(sqrt(real(m))) syntax integer(sqrt(real(m))) incorrect, maybe you mean NINT(sqrt(real(m))) ?

+3
source

The token for "not equal" in fortran is / =.! starts the comment, so line 15 of reading the compiler as

  if (prim(i) 

and therefore confused because there is no bracket to close the boolean expression in the if statement. So just replace! = With / = and it should get rid of this problem.

+2
source

All Articles