# Print all the prime numbers between ‘m’ and ‘n’ in PL/SQL

Prerequisite – PL/SQL Introduction

In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.

**Problem :**

Write a script in PL/SQL to display all the prime numbers between any two positive whole numbers.

**Explanation :**

Here, all the prime numbers between any two numbers taken as input as the upper limit and the lower limit are returned.A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.

In this implementation, the number of divisors of a number that occurs between the two limits is counted which should be 2 (1 and the number itself). If the number of divisors is 2, then that number is returned.

For example, consider the number 5. It has only two divisors: 1 and the number 5 itself.

Hence, it is a prime number.

**Examples :**

Input: 10 20 Output: 11 13 17 19 Input: 20 30 Output: 23 29

**Below is the implementation :**

` ` `DECLARE ` `--the upper limit and the lower limit are taken as user inputs. ` ` ` `low number(2); ` ` ` `high number(2); ` ` ` `n number(2); ` ` ` `m number(2); ` ` ` `c number(20); ` `BEGIN ` ` ` `dbms_output.put_line('Enter the lower and higher limit:'); ` ` ` `low:=&low; ` ` ` `high:=&high; ` `--The main operation happens in this loop ` ` ` `for n IN low.. high ` ` ` `loop ` ` ` `c:=0; ` ` ` `for m IN 1.. n ` ` ` `loop ` ` ` `if mod(n, m)=0 then ` ` ` `c:=c+1; ` ` ` `end if; ` ` ` `end loop; ` `--the number of divisors for each number in the range is counted and then checked. ` ` ` `if c<=2 then ` ` ` `dbms_output.put_line(n||'\n'); ` ` ` `end if; ` ` ` `end loop; ` ` ` `END; ` |

*chevron_right*

*filter_none*

**Output :**

Input: Enter the lower and higher limit:1 10 Output: 2 3 5 7

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Print all even numbers from 1 to n in PL/SQL
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Difference between SQL and PLSQL
- PLSQL | SIN Function
- PLSQL | EXP Function
- PLSQL | TAN Function
- PLSQL | COS Function
- PLSQL : || Operator
- PLSQL | LN Function
- PLSQL | MOD Function
- PLSQL | LOG Function
- PLSQL | ABS Function
- PLSQL | LEAST Function
- PLSQL | CHR Function
- PLSQL | LAST_DAY Function
- PLSQL | CONCAT Function
- PLSQL | UPPER Function
- PLSQL | LPAD Function
- PLSQL | BITAND Function
- PLSQL | INSTRC Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.