Sum of odd numbers using for loop in pl/sql

In this article, we will explore how to write a PL/SQL program to calculate the sum of odd numbers using a FOR loop. We’ll also explain how the logic works and walk through an example that illustrates how to sum the first n odd numbers.

Download Courses for Just $2
πŸŽ‰ These Courses Are FREE! Just Pay a Small $2 Maintenance Fee πŸŽ‰

πŸš€ Get These Premium Courses Now! πŸš€

Tableau Course

$2 Maintenance Fee

⬇ Download

DevOps Course

$2 Maintenance Fee

⬇ Download

Snowflake DB Course

$2 Maintenance Fee

⬇ Download

Power BI Course

$2 Maintenance Fee

⬇ Download

Understanding the Sum of Odd Numbers.

The sum of odd numbers follows a distinct pattern. Here’s how the first few sums work:

  • Sum of the first odd number: 1
  • Sum of the first two odd numbers: 1 + 3 = 4 (which is 2 x 2)
  • Sum of the first three odd numbers: 1 + 3 + 5 = 9 (which is 3 x 3)
  • Sum of the first four odd numbers: 1 + 3 + 5 + 7 = 16 (which is 4 x 4)

In general, the sum of the first n odd numbers equals the square of n. This is known as the odd number theorem. So, if we want to find the sum of the first 10 odd numbers, it would be:

Sum = n x n = 10 x 10 = 100

Writing a PL/SQL Program to Sum Odd Numbers

Below is the PL/SQL code to compute the sum of odd numbers using a FOR loop. The program prompts the user to input the last odd number and then calculates the sum of all odd numbers up to that point.

DECLARE
  num       NUMBER;
  sum_one   NUMBER := 0;
  lastvalue NUMBER;
BEGIN
  lastvalue := &lastvalue;

  FOR n IN 1..lastvalue
  LOOP
    IF MOD(n, 2) = 1 THEN
      sum_one := sum_one + n;
    END IF;
  END LOOP;

  DBMS_OUTPUT.PUT_LINE('Sum = ' || sum_one);
END;
/

Explanation of the Code

  • Line 1-3: We declare three variables. num is used for counting, sum_one initializes the sum to zero, and lastvalue holds the last odd number entered by the user.
  • Line 4-6: The program asks the user for the value of lastvalue, which represents the upper limit for the sum of odd numbers.
  • Line 8-14: A FOR loop is used to iterate through all numbers from 1 to lastvalue. Inside the loop, the MOD function checks if the number is odd (MOD(n, 2) = 1), and if true, it adds the number to sum_one.
  • Line 15: Finally, the DBMS_OUTPUT.PUT_LINE function displays the sum of the odd numbers.

Output Example

Here’s what the output looks like when the value of last value is set to 9:

SQL> /
Enter value for lastvalue: 9
old   6: lastvalue := &lastvalue;
new   6: lastvalue := 9;

Sum = 25

In this case, the program computes the sum of odd numbers from 1 to 9, which are 1 + 3 + 5 + 7 + 9 = 25.

Why This Works

The program uses the odd number theorem, which states that the sum of the first n odd numbers is equal to nΒ². So for n = 5 (odd numbers 1 to 9), the sum is 5 x 5 = 25, confirming the result of our PL/SQL program.

Conclusion

This PL/SQL program offers a simple and efficient way to calculate the sum of odd numbers using a FOR loop. Understanding this approach can help improve your logic when working with loops, iterations, and mathematical patterns in PL/SQL. By using this program, you can easily calculate the sum of any number of odd integers.