Skip to content
Related Articles
Open in App
Not now

Related Articles

Centered triangular number in PL/SQL

Improve Article
Save Article
Like Article
  • Last Updated : 31 Oct, 2018
Improve Article
Save Article
Like Article

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 n and task is to find nth centered triangular number. A centered triangular number is a centered number that represents a triangle with a dot in the center and all other dots surrounding the center in successive triangular layers.

Input: n = 6 
Output: 64

Input: n = 10
Output: 166

The first few centered triangular number series are:
1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 199, 235, 274, 316, 361, 409, 460………………………

nth Term of centered triangular number is given by:


Below is the required implementation:

--PL/SQL Program to find the nth Centered triangular number 
-- declaration section 
  x NUMBER; 
  n NUMBER; 
  --utility function 
  FUNCTION Centered_triangular_num(n IN NUMBER) 
    z NUMBER; 
    --formula applying 
    z := (3 * n * n + 3 * n + 2) / 2; 
    RETURN z; 
  --driver code 
    n := 3; 
    x := centered_triangular_num(n); 
    n := 12; 
    x := centered_trigunal_num(n); 
  --End of program




My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!