Ashutosh Tripathi
Ashutosh Tripathi

Reputation: 13

Error in PL/SQL code and cannot understand the error and mistake in the code

declare

sum number:=0;

count number:=0;

pnum number:=0;

temp number;

begin

for i in 1..25

loop

    temp:=i;
    count:=0;

    for j in 1..25
    loop

        if mod(i,j)=0 then
            count:=count+1;
        end if;

    end loop;

    if count=2 then
        sum:=sum+temp;
        pnum:=pnum+1;
    end if;

    exit when pnum=10;

end loop;

dbms_output.put_line(sum);

end;

Error encountered on Oracle server:

ORA-06550: line 21, column 17: PLS-00103: Encountered the symbol "+" when expecting one of the following:

(

ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_180200", line 548 ORA-06550: line 28, column 25: PLS-00103: Encountered the symbol ")" when expecting one of the following:

(

Upvotes: 0

Views: 289

Answers (1)

Aleksej
Aleksej

Reputation: 22969

You are using reserved words SUM and COUNT; if you edit the name of your variables your code will work:

DECLARE
    vSUM                                     NUMBER := 0;

    vCOUNT                                   NUMBER := 0;

    pnum                                    NUMBER := 0;

    temp                                    NUMBER;
BEGIN
    FOR i IN 1 .. 25
    LOOP
        temp     := i;
        vCOUNT    := 0;

        FOR j IN 1 .. 25
        LOOP
            IF MOD(i, j) = 0
            THEN
                vCOUNT    := vCOUNT + 1;
            END IF;
        END LOOP;

        IF vCOUNT = 2
        THEN
            vSUM     := vSUM + temp;
            pnum    := pnum + 1;
        END IF;

        EXIT WHEN pnum = 10;
    END LOOP;

    DBMS_OUTPUT.put_line(vSUM);
END;

Upvotes: 3

Related Questions