Forum Stats

  • 3,770,497 Users
  • 2,253,126 Discussions
  • 7,875,486 Comments

Discussions

Code correction - PL/SQL

Albert Chao
Albert Chao Member Posts: 131 Green Ribbon
edited Jul 28, 2021 2:34PM in SQL & PL/SQL

Can someone please correct the code below. The code is to find perfect numbers between 1 to 100


set serveroutput on;

DECLARE

  sum NUMBER;

  p  NUMBER;

BEGIN 

dbms_output.put_line('Perfect numbers between 1 and 100 are :'); 

p := 1;

FOR i IN i..100


LOOP 

while(p<=(i/2))

end loop;

if(mod(i,p)=0) then

sum := sum +p;

p := p+1;

end if;

end loop; 

          if(sum = i) 

          dbms_output.put_line ( i|| ' ' ) ; 

          SUM := 0 ;

END ;

Tagged:

Answers

  • Angus123
    Angus123 Member Posts: 100 Red Ribbon

    You should move this post to the pl/sql forum

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,233 Red Diamond

    Hi, @User_3GK1M

    Can someone please correct the code below. 

    • SUM is an Oracle keyword; it's the name of a built-in function. Don't try to use Oracle keywords as variable names.
    • In FOR i IN i..100 the second i is undefined. I think you meant 1, not i.
    • There must be a one-to-one correspondence between LOOP and END LOOPs. You have only one LOOP, but two END LOOPs.
    • Every IF must be followed by THEN and END IF; the second IF in your code has neither.


    Never write that much code at once. Take baby steps. Write as little code as possible and test that little bit. If it is not doing exactly what you expect, fix it and test again. When it is doing exactly what you want, then add one or two more lines of code, and test again. In this case, start with an anonymous block that just displays the message 'Perfect numbers between 1 and 100 are :'). When you get that much working, add a LOOP for the integers 1 to 100. Make sure that is working. (You may have to add some put_line calls that you do not want in the final version.) When that is working, add an inner loop for the integers bewteen 1 and i/2. Test again. If you get stuck, post a question with the previous version of your code (the version that did exactly what you expected) and your current version. Explain what you're trying to do in the current version, and what the problem is.

  • Albert Chao
    Albert Chao Member Posts: 131 Green Ribbon

    @Frank Kulash Please help me getting this code resolved

    set serveroutput on;

    DECLARE

      sumo NUMBER;

      p  NUMBER;

    BEGIN 

    dbms_output.put_line('Perfect numbers between 1 and 100 are :'); 

    p := 1;

    LOOP

    FOR i IN 1..100

    while(p<=(i/2))

    if(mod(i,p)=0) then

    sumo := sumo +p;

    p := p+1;

    end if;

    end loop; 

              if(sum = i)  then

              dbms_output.put_line ( i|| ' ' ) ; 

              sumo := 0 ;

    END ;

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,233 Red Diamond

    Hi, @User_3GK1M

    Please help me getting this code resolved

    I'm trying. Are you taking baby steps? Post the last version of your code (that is, the version that is only a couple of lines shorter) that did exactly what you expected, and explain what this version is supposed to do.

  • Albert Chao
    Albert Chao Member Posts: 131 Green Ribbon

    @Frank Kulash Yes, I am trying baby steps but I am getting error like that "Encounter the symbol "WHILE" when expecting one of the following". Actually, I wanted to generate the perfect numbers between 1 to 100.

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,233 Red Diamond

    Hi,

    Yes, I am trying baby steps

    Are you really? It looks like you're trying to fix the code you posted before, and not starting over from the beginning.

    but I am getting error like that "Encounter the symbol "WHILE" when expecting one of the following"

    Okay, so you're having trouble adding WHILE. If you;re really taking baby steps, then one more time: Post the earlier version of your code, the version that didn't have WHILE in it, that did exactly what you expected. Explain what you're trying to do in this step.