Finding sum of first n natural numbers in PL/SQL


In this problem, we are given a number N. Our task is to finding sum of first n natural numbers in PL/SQL.

PL/SQL is a combination of SQL along with the procedural features of programming languages.

PL/SQL has the following features −

  • PL/SQL is tightly integrated with SQL.

  • It offers extensive error checking.

  • It offers numerous data types.

  • It offers a variety of programming structures.

  • It supports structured programming through functions and procedures.

  • It supports object-oriented programming.

  • It supports the development of web applications and server pages.

PL/SQL has the following advantages −

  • SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations and transaction control from PL/SQL block. In Dynamic SQL, SQL allows embedding DDL statements in PL/SQL blocks.

  • PL/SQL allows sending an entire block of statements to the database at one time. This reduces network traffic and provides high performance for the applications.

  • PL/SQL gives high productivity to programmers as it can query, transform, and update data in a database.

  • PL/SQL saves time on design and debugging by strong features, such as exception handling, encapsulation, data hiding, and object-oriented data types.

  • Applications written in PL/SQL are fully portable.

  • PL/SQL provides high security level.

  • PL/SQL provides access to predefined SQL packages.

  • PL/SQL provides support for Object-Oriented Programming.

  • PL/SQL provides support for developing Web Applications and Server Pages.

Let's take an example to understand the problem,

Input: N = 6
Output: 21

Solution Approach

PL/SQL works just like another programming language and the algorithm to find the number of first n natural numbers is the same. For finding it we have two methods.

Method 1

One method to solve the problem is by using a sum variable and adding each value from 1 to N to the sum. The values of sum after all additions are done is the result.

Example

Program to illustrate the working of our solution

DECLARE
sumVal NUMBER;
n NUMBER;
i NUMBER;

FUNCTION Findmax(n IN NUMBER)
   RETURN NUMBER
IS
   sums NUMBER := 0;
BEGIN
   FOR i IN 1..n
   LOOP
   sums := sums + i*(i+1)/2;
   END LOOP;
   RETURN sums;
END;
BEGIN
   n := 8;
   sumVal := findmax(n);
   dbms_output.Put_line('Sum of natural numbers is ' || sumVal);
END;

Output

Sum of natural numbers is 36

Method 2

An alternate method to solve the problem is by using the general formula to find the sum of the first N natural numbers. The same can be implemented with PL/SQL.

The formula for finding the sum of first N natural numbers is $(N^*(N+1)(N+2))/6$.

Example

Program to illustrate the working of our solution

DECLARE
sumNum NUMBER;
N NUMBER;

FUNCTION Findmax(N IN NUMBER)
   RETURN NUMBER
IS
   sumVal NUMBER;
BEGIN
   sumVal := (N * (N + 1) * (N + 2)) / 6;
   RETURN sumVal;
END;
BEGIN
   N := 8;
   sumNum := findmax(N);
   dbms_output.Put_line('Sum of natural numbers is ' || sumNum);
END;

Output

Sum of natural numbers is 36

Updated on: 01-Feb-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements