w3resource

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:

Flowchart: Check whether a number is prime or not using goto statement with for loop.

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?



Follow us on Facebook and Twitter for latest update.