
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- 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 pyramid of tutorialspoint in PL/SQL
- Count odd and even digits in a number in PL/SQL
- Print numbers with digits 0 and 1 only such that their sum is N in C Program.
- Program for Fibonacci numbers in PL/SQL
- Swift Program to calculate the sum of all odd numbers up to N
- Haskell Program to calculate the sum of all odd numbers up to N
- Golang program to calculate the sum of all odd numbers up to N
- C++ Program to calculate the sum of all odd numbers up to N
- Print prime numbers from 1 to N in reverse order
- Difference between SQL and PL/SQL
- Print n numbers such that their sum is a perfect square
- Python program to print all odd numbers in a range

Advertisements