-1

Im trying to write a program in PL/SQL for printing prime numbers between 1 to 50 for that i use the following code-

DECLARE
    i number(3);
    j number(3);
BEGIN
    i := 2;
    LOOP
        j := 2;
        LOOP
            EXIT WHEN((MOD(I,J) =0) OR (j=i));
            j := j+1
        END LOOP
        IF LOOP;
        IF(j=i) THEN
            dbms_output,put(i); **//This line do nothing** 
        END IF;
        i := i+1
        exit WHEN i=50;
    END LOOP;
END;

but is doesn't desplay anything.

I'm using Oracledb Standalone compiler/editor to run it.can anyone tell me what was the problem with that.

2
  • 2
    Your code has too much syntax errors and it cannot display anything, because it even doesn't compile. Please, edit the code, provide the error you have and describe what is the issue with it. And this code has nothing about Google geocoder Commented Feb 19, 2022 at 10:56
  • One of the many problems you have that prevent the procedure from even compiling is that you have 'dbms_output (comma) put'. That comma should be a period. Seriously, you need to post not only the code, but a copy and paste of what you actually executed and the output it generated. I'm sure there were error messages that you should have reported. Commented Mar 1, 2022 at 14:58

1 Answer 1

2

I haven't heard of this "Oracledb Standalone compiler/editor" that you are using, so consult the product documentation for how to enable DBMS Output, as it's not always enabled by default. You may need to find a menu bar checkbox or include a set server output on command before the start of the PL/SQL block.

Regarding the code, there are several issues that prevent it from compiling. Each statement must be terminated with a semicolon. Also to print a complete line with linefeed, you need to call dbms_output.put_line, because dbms_output.put only adds characters (to allow you to build a line incrementally, for example) and must be followed by put_line or new_line in order to output anything. I'm not sure what IF LOOP; is meant to do.

To separate your output display issue from the program logic for generating prime numbers, I recommend simplifying the code down to the simplest test case that reproduces the issue, for example,

begin
    dbms_output.put_line('Hello, world!');
end;

Can you run this? Does your editor display output?

Here is a fixed version of your code:

declare
    i number(3) := 2;
    j number(3);
begin
    -- i := 2;  - moved to declaration
    while i < 50 loop
        j := 2;

        loop
            exit when((mod(i, j) = 0) or (j = i));
            j := j + 1; -- missing semicolon
        end loop; -- missing semicolon

        if j = i then
            dbms_output.put_line(i); -- put_line prints a line. put requires a newline
        end if;

        i := i + 1; -- missing semicolon

        -- exit when i = 50;  -- moved to loop definition
    end loop;
end;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.