Write a program in PL/SQL to show the uses of nested loop.
SOLUTION FOR THE ABOVE QUESTION
In this question, to illustrate the uses of nested loop we will write an SQL program to find the prime numbers between 2 and 50.
DECLARE
my_var_1 number(3);
my_var_2 number(3);
BEGIN
/*initialize my_var_1 to 2 */
my_var_1 := 2;
LOOP
/*initialize my_var_2 to 2 */
my_var_2:= 2;
LOOP
exit WHEN ((mod(my_var_1, my_var_2) = 0) or (my_var_2 = my_var_1));
/*Increment the my_var_2 by 1
my_var_2 := my_var_2 + 1;
END LOOP;
/*If a number is prime print it*/
IF (my_var_2 = my_var_1 ) THEN
dbms_output.put_line(my_var_1 || ' is prime');
END IF;
/*Increment the my_var_1 by 1
my_var_1 := my_var_1 + 1;
/*Exit the program when the value of my_var_1 is 50
exit WHEN my_var_1 = 50;
END LOOP;
END;
Comments
Leave a comment