- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Print all odd numbers and their sum from 1 to n in PL/SQL

In this problem, we are given a number n and we have to print all odd numbers from 1 to n and also print the sum of numbers from 1 to n in **PL/SQL**.

**PL/SQL** is a procedural language extension to SQL. The code is a sequence of instructions that are ground in a block with all related declarations and instructions.

Let’s see an example of our problem −

Input: 7 Output: odd numbers are: 1, 3, 5, 7 Sum of odd numbers is 16

To solve this problem, we will take a number and initialize it to 1 and a sum variable with initial value 0. And we will increase the number by 2 and add into the sum variable until its value is less than or equal to n.

## Example

DECLARE number NUMBER(3) := 1; sumvar NUMBER(4) := 0; BEGIN dbms_output.Put_line('The odd numbers are : '); WHILE num <= 7 LOOP dbms_output.Put_line(number); sumvar := sumvar+num; num := num + 2; END LOOP; dbms_output.Put_line('Sum of odd numbers is '|| sum1); END;

## Output

The odd numbers are −

1 3 5 7 Sum of odd numbers is 16

- Related Questions & Answers
- Finding sum of first n natural numbers in PL/SQL
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Compute sum of digits in all numbers from 1 to n
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Print prime numbers from 1 to N in reverse order
- Print pyramid of tutorialspoint in PL/SQL
- Print n numbers such that their sum is a perfect square
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Count odd and even digits in a number in PL/SQL
- Difference between SQL and PL/SQL
- Program for Fibonacci numbers in PL/SQL
- Squared sum of n odd numbers - JavaScript
- Difference Between T-SQL and PL-SQL
- Java Program to Display All Prime Numbers from 1 to N
- C Program to print numbers from 1 to N without using semicolon

Advertisements