# Print all odd numbers and their sum from 1 to 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.

Given a number N, the task is to display all the odd numbers from 1 to N and their sum.

Examples:

Input: 3 Output: 1, 3 Input: 5 Output: 1, 3, 5

Approach is to initialize a *num* with 1 and *sum* with 0 and keep incrementing *num* by 2 and *sum* by *num* until num <= N.

`-- display all odd number from 1 to n ` `DECLARE` ` ` `-- declare variable num ` ` ` `num NUMBER(3) := 1; ` ` ` `sum1 NUMBER(4) := 0; ` `BEGIN` ` ` `WHILE num <= 5 LOOP ` ` ` `-- display odd number ` ` ` `dbms_output.Put_line(num); ` ` ` ` ` `-- the sum of all odd numbers ` ` ` `sum1 := sum1 + num; ` ` ` ` ` `--next odd number ` ` ` `num := num + 2; ` ` ` ` ` `-- end loop ` ` ` `END` `LOOP; ` `dbms_output.Put_line(` `'Sum of all odd numbers is '` `|| sum1); ` `END` `; ` |

*chevron_right*

*filter_none*

Output:

1 3 5 Sum of all odd numbers is 9

## Recommended Posts:

- Print all even numbers from 1 to n in PL/SQL
- Print all the prime numbers between 'm' and 'n' in PL/SQL
- PLSQL : || Operator
- PLSQL | TAN Function
- Difference between SQL and PLSQL
- PLSQL | SIN Function
- PLSQL | LEAST Function
- PLSQL | EXP Function
- PLSQL | COS Function
- PLSQL | MOD Function
- PLSQL | ABS Function
- PLSQL | CHR Function
- PLSQL | LN Function
- PLSQL | LOG Function
- PLSQL | RTRIM Function
- PLSQL | TRUNC Function
- PLSQL | CURRENT_TIMESTAMP Function
- PLSQL | NCHR Function
- PLSQL | GREATEST Function
- PLSQL | SOUNDEX 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.