Centered triangular number
Last Updated :
29 Mar, 2023
Given an integer n, find the nth Centered triangular number.
Centered Triangular Number is a centered polygonal number that represents a triangle with a dot in the center and all other dots surrounding the center in successive triangular layers [Source : Wiki ]
Pictorial Representation :

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.............................
Examples:
Input : n = 1
Output : 4
Explanation :
A dot in the center and 3 dots forming the
triangle outside it, thus 4.
Input : n = 6
Output : 64
Input : n = 10
Output : 166
Approach
nth Term of centered triangular number is given by:
CT_{n}=(3n^2+3n+2)/2
Basic Implementation of the above approach:
C++
// C++ Program to find the
// nth Centered Triangular number
#include <iostream>
using namespace std;
// function for Centered
// Triangular number
int Centered_Triangular_num(int n)
{
// formula for find Centered
// Triangular number nth term
return (3 * n * n + 3 * n + 2) / 2;
}
// Driver Code
int main()
{
// For 3rd Centered Triangular number
int n = 3;
cout << Centered_Triangular_num(n) << endl;
// For 12th Centered Triangular number
n = 12;
cout << Centered_Triangular_num(n) << endl;
return 0;
}
C
// C Program to find the
// nth Centered Triangular number
#include <stdio.h>
// function for Centered
// Triangular number
int Centered_Triangular_num(int n)
{
// formula for find Centered
// Triangular number nth term
return (3 * n * n + 3 * n + 2) / 2;
}
// Driver Code
int main()
{
// For 3rd Centered Triangular number
int n = 3;
printf("%d\n",Centered_Triangular_num(n));
// For 12th Centered Triangular number
n = 12;
printf("%d\n",Centered_Triangular_num(n));
return 0;
}
// This code is contributed by kothavvsaakash.
Java
// Java Program to find
// the nth Centered
// Triangular number
import java.io.*;
class GFG
{
// function for Centered
// Triangular number
static int Centered_Triangular_num(int n)
{
// formula for find Centered
// Triangular number nth term
return (3 * n * n +
3 * n + 2) / 2;
}
// Driver Code
public static void main (String[] args)
{
// For 3rd Centered
// Triangular number
int n = 3;
System.out.println(Centered_Triangular_num(n));
// For 12th Centered
// Triangular number
n = 12;
System.out.println(Centered_Triangular_num(n));
}
}
// This code is contributed by ajit
Python3
# Program to find nth
# Centered Triangular number
def Centered_Triangular_num(n) :
# Formula to calculate nth
# Centered Triangular number
return (3 * n * n +
3 * n + 2) // 2
# Driver Code
if __name__ == '__main__' :
# For 3rd Centered
# Triangular number
n = 3
print(Centered_Triangular_num(n))
# For 12th Centered
# Triangular number
n = 12
print(Centered_Triangular_num(n))
# This code is contributed
# by akt_mit
C#
// C# Program to find
// the nth Centered
// Triangular number
using System;
class GFG
{
// function for Centered
// Triangular number
static int Centered_Triangular_num(int n)
{
// formula for find Centered
// Triangular number nth term
return (3 * n * n +
3 * n + 2) / 2;
}
// Driver Code
static public void Main ()
{
// For 3rd Centered
// Triangular number
int n = 3;
Console.WriteLine(Centered_Triangular_num(n));
// For 12th Centered
// Triangular number
n = 12;
Console.WriteLine(Centered_Triangular_num(n));
}
}
// This code is contributed by akt_mit
PHP
<?php
// PHP Program to find the
//nth Centered Triangular number
// function for Centered
// Triangular number
function Centered_Triangular_num($n)
{
// formula for find Centered
// Triangular number nth term
return (3 * $n * $n + 3 * $n + 2) / 2;
}
// Driver Code
// For 3rd Centered Triangular number
$n = 3;
echo Centered_Triangular_num($n), "\n" ;
// For 12th Centered Triangular number
$n = 12;
echo Centered_Triangular_num($n), "\n";
// This code is contributed by aj_36
?>
JavaScript
<script>
// javascript Program to find
// the nth Centered
// Triangular number
// function for Centered
// Triangular number
function Centered_Triangular_num(n)
{
// formula for find Centered
// Triangular number nth term
return (3 * n * n + 3 * n + 2) / 2;
}
// Driver Code
// For 3rd Centered
// Triangular number
var n = 3;
document.write(Centered_Triangular_num(n)+"<br/>");
// For 12th Centered
// Triangular number
n = 12;
document.write(Centered_Triangular_num(n)+"<br/>");
// This code is contributed by Rajput-Ji
</script>
Output :
19
235
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Centered triangular number in PL/SQL 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 num
2 min read
Centered tridecagonal number Given a number n, the task is to find the nth Centered Tridecagonal Number. A Centered tridecagonal number represents a dot at the center and other dots surrounding the center dot in the successive tridecagonal(13 sided polygon) layer. Examples :  Input : 2 Output : 14 Input : 9 Output : 469   Fo
3 min read
Centered Square Number Given a number n, the task is to find nth Centered Square Number. A centered Square Number is a centered figurate number that gives the number of dots in a square with a dot in the center and all other dots surrounding the center dot in successive square layers. Nth Centered square number can be cal
5 min read
Centered Octagonal Number Given a number n, find the nth centered octagonal number. A centered octagonal number represents an octagon with a dot in the center and others dots surrounding the center dot in the successive octagonal layer. Examples : Input : 2 Output : 9 Input : 5 Output : 81 Centered Octagonal n-th Number is g
4 min read
Centered Octahedral number We are given a number n, we need to find n-th centered octahedral number.Description: A centered octahedral number is a figurate number. It counts the number of points of a three-dimensional integer lattice that lie inside an octahedron centered at the origin. The same numbers are special cases of t
4 min read
Centered pentagonal number Given an integer n, find the nth Centered pentagonal number. A Centered Pentagonal Number is a centered figurate number that represents a pentagon with a dot in the center and other dots surrounding it in pentagonal layers successively [ Source: Wiki ] Few Centered pentagonal Number are : 1, 6, 16,
4 min read
Centered tetrahedral number We are given integer n, we need to find n-th centered tetrahedral number.Description: The centered tetrahedral number is a centered figurate number that represents a tetrahedron.Tetrahedral Numbers: A number is termed as a tetrahedral number if it can be represented as a pyramid with a triangular ba
4 min read
Centered Octadecagonal Number Given a number n, find the nth Centered Octadecagonal number. The Centered Octadecagonal Number represents a dot in the center and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). Examples : Input : 2 Output : 19 Input : 6 Output : 271 In mathematics, Centered
4 min read
Centered Pentadecagonal Number Given a number n, find the nth Centered Pentadecagonal Number . A Centered Pentadecagonal Number represents a dot in the center and other dots surrounding it in successive pentadecagonal(15-sided polygon) layers. Examples : Input : 2 Output : 16 Input : 8 Output : 421 n-th term of Centered pentadeca
4 min read
Centered cube number Given a number n, find the n-th centered cube number.The Centered cube number counts the number of points which are formed by a point that is surrounded by concentric cubical layers in 3D with i2 points on the square faces of the i-th layer. Source[WIKI]. Please see this image for more clarity.The f
5 min read