PL/SQL Control Statement Exercises: Check whether a number is prime or not using goto statement with for loop
PL/SQL Control Statement: Exercise-28 with Solution
Write a program in PL/SQL to check whether a number is prime or not using goto statement with for loop.
Sample Solution:
PL/SQL Code:
DECLARE
msg VARCHAR2(30);
n PLS_INTEGER := 83;
BEGIN
FOR i in 2..ROUND(SQRT(n)) LOOP
IF n MOD i = 0 THEN
msg := ' is not a prime number';
GOTO when_prime;
END IF;
END LOOP;
msg := ' is a prime number';
<>
DBMS_OUTPUT.PUT_LINE(TO_CHAR(n) || msg);
END;
/
Flowchart:
Sample Output:
83 is a prime number PL/SQL procedure successfully completed.
Improve this sample solution and post your code through Disqus
Previous: Write a program in PL/SQL to print the prime numbers between 1 to 50.
Next: Write a program in PL/SQL to insert records from one table to another.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/plsql-exercises/control-statement/plsql-control-statement-exercise-28.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics