PL/SQL Control Statement Exercises: Print the prime numbers between 1 to 50
PL/SQL Control Statement: Exercise-27 with Solution
Write a program in PL/SQL to print the prime numbers between 1 to 50.
Sample Solution:
PL/SQL Code:
DECLARE
i NUMBER(3);
j NUMBER(3);
BEGIN
dbms_output.Put_line('The prime numbers are:');
dbms_output.new_line;
i := 2;
LOOP
j := 2;
LOOP
EXIT WHEN( ( MOD(i, j) = 0 )
OR ( j = i ) );
j := j + 1;
END LOOP;
IF( j = i )THEN
dbms_output.Put(i||' ');
END IF;
i := i + 1;
exit WHEN i = 50;
END LOOP;
dbms_output.new_line;
END;
/
Flowchart:

Sample Output:
The prime numbers are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 PL/SQL procedure successfully completed.
Improve this sample solution and post your code through Disqus
Previous: Write a program in PL/SQL to explain the uses of nested for loop with label.
Next: Write a program in PL/SQL to check whether a number is prime or not using goto statement with for loop.
What is the difficulty level of this exercise?
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook