Answer to Question #258554 in Databases | SQL | Oracle | MS Access for Tarurendra Kushwah

Question #258554

Write a program in PL/SQL to show the uses of nested loop.


1
Expert's answer
2021-10-29T12:16:48-0400


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; 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS