Prime number code - please help me in solving this error ('missing if'?)
SQL> ed
Wrote file afiedt开发者_开发问答.buf
1 declare
2 n number;
3 i number;
4 counter number;
5 begin
6 n:=&n;
7 i:=1;
8 counter:=0;
9 if n=1
10 then dbms_output.put_line('1 is a prime No.');
11 else if n=2
12 then dbms_output.put_line('2 is even prime');
13 else
14 for i in 1..n loop
15 if mod(n,i)=0
16 then counter:=counter+1;
17 end if;
18 end loop;
19 end if;
20 if counter=2
21 then dbms_output.put_line(n||' is a prime No.');
22 else
23 dbms_output.put_line(n||' is a not prime No.');
24 end if;
25* end
I get the following error which I don't understand. Can anyone explain what's causing it?
SQL> /
Enter value for n: 8
old 6: n:=&n;
new 6: n:=8;
end
*
ERROR at line 25:
ORA-06550: line 25, column 3:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the
following:
if
It's because of this code segment, formatted to give you a better understnding why:
9 if n=1
10 | then dbms_output.put_line('1 is a prime No.');
11 else
| if n=2
12 | | then dbms_output.put_line('2 is even prime');
13 | else
14 | | for i in 1..n loop
15 | | | if mod(n,i)=0
16 | | | | then counter:=counter+1;
17 | | | end if;
18 | | end loop;
19 | end if;
?
In other words, that section is missing an end if
. That's why, when you finish your file on line 25 with end
, it complains about not having an if
after it (to make end if
).
Just whack an end if
immediately following line 19 and that should fix it.
That should fix your immediate problem but I have a couple of quick comments as well.
- if you use
for i in 2..n loop
(start at 2 instead of 1), and testcounter
being greater than 1 (instead of equal to 2), that should save some work.mod(n,1)
is zero for alln
. - in any case, the original test should have been for greater than 2, not equal: the number 12 would have given you a
counter
of 5 (one each for 1, 2, 3, 4 and 6) and so would be considered non-prime. - for efficiency, you should remember that you only ever have to check up to
trunc(sqrt(n))+1
(or the equivalent in whatever language you're using) since, if a number higher than that is a factor, you would have found its pair already: 12 mod 4 is zero but you've already found its pair of 3 (12 mod (12/4) is zero). - make sure that the
2..n
does not includen
since themod
operation there will also increment counter (I don't know how your specific language handles that loop construct) - it may have to be2..n-1
.
I think you're looking for the PL/SQL ELSIF
keyword. I took your code, replaced
else if n=2
on line 11 with
elsif n=2
and it worked.
For every IF
we should close it by using END IF;
But when we use ELSEIF
one is enough.
IF
......
ELSEIF
......
END IF;
or
IF
......
ELSE IF
.......
END IF;
END IF;
1) You need a semicolon after the final end
at line 25. 2) Then you get another error, (See note 1 below) and need to correct with by substituting elsif
for else if
at line 11. 3) Finaly, 1 not being prime, line 10 needs correction also, then dbms_output.put_line('1 is neither prime nor composite.);
So the corrected code is:
SQL> declare
2 n number;
3 i number;
4 counter number;
5 begin
6 n:=&n;
7 i:=1;
8 counter:=0;
9 if n=1
10 then dbms_output.put_line('1 is neither prime nor composite.');
11 elsif n=2
12 then dbms_output.put_line('2 is even prime');
13 else
14 for i in 1..n loop
15 if mod(n,i)=0
16 then counter:=counter+1;
17 end if;
18 end loop;
19 end if;
20 if counter=2
21 then dbms_output.put_line(n||' is a prime No.');
22 else
23 dbms_output.put_line(n||' is a not prime No.');
24 end if;
25 end;
26 /
Enter value for n: 3
old 6: n:=&n;
new 6: n:=3;
3 is a prime No.
PL/SQL procedure successfully completed.
Also, see paxdiablo's answer for additional notes on improving this code with respect to prime numbers.
Note 1: The second syntax error looks like:
SQL> declare
2 n number;
3 i number;
4 counter number;
5 begin
6 n:=&n;
7 i:=1;
8 counter:=0;
9 if n=1
10 then dbms_output.put_line('1 is a prime No.');
11 else if n=2
12 then dbms_output.put_line('2 is even prime');
13 else
14 for i in 1..n loop
15 if mod(n,i)=0
16 then counter:=counter+1;
17 end if;
18 end loop;
19 end if;
20 if counter=2
21 then dbms_output.put_line(n||' is a prime No.');
22 else
23 dbms_output.put_line(n||' is a not prime No.');
24 end if;
25 end;
26 /
Enter value for n: 10
old 6: n:=&n;
new 6: n:=10;
end;
*
ERROR at line 25:
ORA-06550: line 25, column 4:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
if
DECLARE
N NUMBER:=&N;
V_COUNT NUMBER:=0;
BEGIN
FOR I IN 1..N LOOP
IF MOD(N,I)=0 THEN
V_COUNT:=V_COUNT+1;
END IF;
END LOOP;
IF V_COUNT<=2 THEN
DBMS_OUTPUT.PUT_LINE(N||' ' ||'IS PRIME NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE(N||' '||' IS NOT PRIME NUMBER');
END IF;
END;
declare
n number;
i number;
counter number;
begin
n := &n;
i := 1;
counter := 0;
if n = 1 then
dbms_output.put_line('1 is a prime No.');
elsif n = 2 then
dbms_output.put_line('2 is even prime');
else
for i in 1 .. n loop
if mod(n, i) = 0 then
counter := counter + 1;
end if;
end loop;
end if;
if counter = 2 then
dbms_output.put_line(n || ' is a prime No.');
else
dbms_output.put_line(n || ' is a not prime No.');
end if;
end;
prime number checking.
DECLARE
N NUMBER :=&N;
I NUMBER;
COUNTER NUMBER :=0;
BEGIN
FOR I IN 1..N LOOP
IF(MOD(N,I) =0) THEN
COUNTER :=COUNTER+1;
END IF;
END LOOP;
IF (COUNTER =2) THEN
DBMS_OUTPUT.PUT_LINE(N ||' '||'IS PRIME NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE(N ||' '||'IS NOT A PRIME NUMBER');
END IF;
END;
精彩评论