The token for "not equals" in fortran is /= . ! starts a comment, hence the compiler reading line 15 as
if (prim(i)
and so is confused because there is no bracket to close the logical expression in the if statement. So simply replace != with /= and it should get rid of this problem.