Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 109 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Code correction - 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 ;
Answers
-
You should move this post to the pl/sql forum
-
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.
-
@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 ;
-
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.
-
@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.
-
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.