Skip to content
Related Articles

Related Articles

Improve Article
Triangular Numbers
  • Difficulty Level : Basic
  • Last Updated : 26 Mar, 2021

A number is termed as triangular number if we can represent it in the form of triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, second row has two points, third row has three points and so on. The starting triangular numbers are 1, 3 (1+2), 6 (1+2+3), 10 (1+2+3+4).
 

triangular

 

How to check if a number is Triangular? 
The idea is based on the fact that n’th triangular number can be written as sum of n natural numbers, that is n*(n+1)/2. The reason for this is simple, base line of triangular grid has n dots, line above base has (n-1) dots and so on.
  
Method 1 (Simple) 
We start with 1 and check if the number is equal to 1. If it is not, we add 2 to make it 3 and recheck with the number. We repeat this procedure until the sum remains less than or equal to the number that is to be checked for being triangular.
Following is the implementations to check if a number is triangular number. 
 

C++




// C++ program to check if a number is a triangular number
// using simple approach.
#include <iostream>
using namespace std;
 
// Returns true if 'num' is triangular, else false
bool isTriangular(int num)
{
    // Base case
    if (num < 0)
        return false;
 
    // A Triangular number must be sum of first n
    // natural numbers
    int sum = 0;
    for (int n=1; sum<=num; n++)
    {
        sum = sum + n;
        if (sum==num)
            return true;
    }
 
    return false;
}
 
// Driver code
int main()
{
    int n = 55;
    if (isTriangular(n))
        cout << "The number is a triangular number";
    else
        cout << "The number is NOT a triangular number";
 
    return 0;
}

Java




// Java program to check if a
// number is a triangular number
// using simple approach
class GFG
{
     
    // Returns true if 'num' is
    // triangular, else false
    static boolean isTriangular(int num)
    {
        // Base case
        if (num < 0)
            return false;
     
        // A Triangular number must be
        // sum of first n natural numbers
        int sum = 0;
         
        for (int n = 1; sum <= num; n++)
        {
            sum = sum + n;
            if (sum == num)
                return true;
        }
     
        return false;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 55;
        if (isTriangular(n))
            System.out.print("The number "
                + "is a triangular number");
        else
            System.out.print("The number"
             + " is NOT a triangular number");
    }
}
 
// This code is contributed
// by Anant Agarwal.

Python3




# Python3 program to check if a number is a
# triangular number using simple approach.
 
# Returns True if 'num' is triangular, else False
def isTriangular(num):
 
    # Base case
    if (num < 0):
        return False
 
    # A Triangular number must be
    # sum of first n natural numbers
    sum, n = 0, 1
 
    while(sum <= num):
     
        sum = sum + n
        if (sum == num):
            return True
        n += 1
 
    return False
 
# Driver code
n = 55
if (isTriangular(n)):
    print("The number is a triangular number")
else:
    print("The number is NOT a triangular number")
 
# This code is contributed by Smitha Dinesh Semwal.

C#




// C# program to check if a number is a
// triangular number using simple approach
using System;
 
class GFG {
     
    // Returns true if 'num' is
    // triangular, else false
    static bool isTriangular(int num)
    {
        // Base case
        if (num < 0)
            return false;
     
        // A Triangular number must be
        // sum of first n natural numbers
        int sum = 0;
         
        for (int n = 1; sum <= num; n++)
        {
            sum = sum + n;
            if (sum == num)
                return true;
        }
     
        return false;
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 55;
         
        if (isTriangular(n))
            Console.WriteLine("The number "
                + "is a triangular number");
        else
            Console.WriteLine("The number"
            + " is NOT a triangular number");
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP program to check if a number is a
// triangular number using simple approach.
 
// Returns true if 'num' is triangular,
// else false
function isTriangular( $num)
{
     
    // Base case
    if ($num < 0)
        return false;
 
    // A Triangular number must be
    // sum of first n natural numbers
    $sum = 0;
    for ($n = 1; $sum <= $num; $n++)
    {
        $sum = $sum + $n;
        if ($sum == $num)
            return true;
    }
 
    return false;
}
 
// Driver code
$n = 55;
if (isTriangular($n))
    echo "The number is a triangular number";
else
    echo "The number is NOT a triangular number";
     
// This code is contributed by Rajput-Ji
?>

Javascript




<script>
// javascript program to check if a number is a triangular number
// using simple approach.
 
// Returns true if 'num' is triangular, else false
function isTriangular(num)
{
 
    // Base case
    if (num < 0)
        return false;
 
    // A Triangular number must be sum of first n
    // natural numbers
    let sum = 0;
    for (let n = 1; sum <= num; n++)
    {
        sum = sum + n;
        if (sum == num)
            return true;
    }
 
    return false;
}
 
// Driver code
let n = 55;
    if (isTriangular(n))
       document.write( "The number is a triangular number");
    else
       document.write( "The number is NOT a triangular number");
        
// This code is contributed by aashish1995
 
</script>

Output: 
 



 The number is a triangular number 

  
 
Method 2 (Using Quadratic Equation Root Formula) 
We form a quadratic equation by equating the number to the formula of sum of first ‘n’ natural numbers, and if we get atleast one value of ‘n’ that is a natural number, we say that the number is a triangular number. 
 

Let the input number be 'num'. We consider,

n*(n+1) = num

as,

 n2 + n + (-2 * num) = 0 

Below is the implementation of above idea.
 

C++




// C++ program to check if a number is a triangular number
// using quadratic equation.
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if num is triangular
bool isTriangular(int num)
{
    if (num < 0)
        return false;
 
    // Considering the equation n*(n+1)/2 = num
    // The equation is  : a(n^2) + bn + c = 0";
    int c = (-2 * num);
    int b = 1, a = 1;
    int d = (b * b) - (4 * a * c);
 
    if (d < 0)
        return false;
 
    // Find roots of equation
    float root1 = ( -b + sqrt(d)) / (2 * a);
    float root2 = ( -b - sqrt(d)) / (2 * a);
 
    // checking if root1 is natural
    if (root1 > 0 && floor(root1) == root1)
        return true;
 
    // checking if root2 is natural
    if (root2 > 0 && floor(root2) == root2)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int num = 55;
    if (isTriangular(num))
        cout << "The number is a triangular number";
    else
        cout << "The number is NOT a triangular number";
 
    return 0;
}

Java




// Java program to check if a number is a
// triangular number using quadratic equation.
import java.io.*;
 
class GFG {
 
    // Returns true if num is triangular
    static boolean isTriangular(int num)
    {
        if (num < 0)
            return false;
     
        // Considering the equation
        // n*(n+1)/2 = num
        // The equation is :
        // a(n^2) + bn + c = 0";
        int c = (-2 * num);
        int b = 1, a = 1;
        int d = (b * b) - (4 * a * c);
     
        if (d < 0)
            return false;
     
        // Find roots of equation
        float root1 = ( -b +
           (float)Math.sqrt(d)) / (2 * a);
            
        float root2 = ( -b -
           (float)Math.sqrt(d)) / (2 * a);
     
        // checking if root1 is natural
        if (root1 > 0 && Math.floor(root1)
                                  == root1)
            return true;
     
        // checking if root2 is natural
        if (root2 > 0 && Math.floor(root2)
                                  == root2)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void main (String[] args) {
        int num = 55;
        if (isTriangular(num))
            System.out.println("The number is"
                    + " a triangular number");
        else
            System.out.println ("The number "
              + "is NOT a triangular number");
    }
}
 
//This code is contributed by vt_m.

Python3




# Python3 program to check if a number is a
# triangular number using quadratic equation.
import math
 
# Returns True if num is triangular
def isTriangular(num):
 
    if (num < 0):
        return False
 
    # Considering the equation n*(n+1)/2 = num
    # The equation is : a(n^2) + bn + c = 0
    c = (-2 * num)
    b, a = 1, 1
    d = (b * b) - (4 * a * c)
 
    if (d < 0):
        return False
 
    # Find roots of equation
    root1 = ( -b + math.sqrt(d)) / (2 * a)
    root2 = ( -b - math.sqrt(d)) / (2 * a)
 
    # checking if root1 is natural
    if (root1 > 0 and math.floor(root1) == root1):
        return True
 
    # checking if root2 is natural
    if (root2 > 0 and math.floor(root2) == root2):
        return True
 
    return False
 
 
# Driver code
n = 55
if (isTriangular(n)):
    print("The number is a triangular number")
else:
    print("The number is NOT a triangular number")
 
# This code is contributed by Smitha Dinesh Semwal

C#




// C# program to check if a number is a triangular
// number using quadratic equation.
using System;
 
class GFG {
     
    // Returns true if num is triangular
    static bool isTriangular(int num)
    {
        if (num < 0)
            return false;
     
        // Considering the equation n*(n+1)/2 = num
        // The equation is : a(n^2) + bn + c = 0";
        int c = (-2 * num);
        int b = 1, a = 1;
        int d = (b * b) - (4 * a * c);
     
        if (d < 0)
            return false;
     
        // Find roots of equation
        float root1 = ( -b + (float)Math.Sqrt(d))
                                        / (2 * a);
                                         
        float root2 = ( -b - (float)Math.Sqrt(d)) 
                                        / (2 * a);
     
        // checking if root1 is natural
        if (root1 > 0 && Math.Floor(root1) == root1)
            return true;
     
        // checking if root2 is natural
        if (root2 > 0 && Math.Floor(root2) == root2)
            return true;
     
        return false;
    }
     
    // Driver code
    public static void Main () {
         
        int num = 55;
        if (isTriangular(num))
            Console.WriteLine("The number is a "
                            + "triangular number");
        else
            Console.WriteLine ("The number is NOT "
                          + "a triangular number");
    }
}
 
//This code is contributed by vt_m.

PHP




<?php
// PHP program to check if a number is a
// triangular number using quadratic equation.
 
// Returns true if num is triangular
function isTriangular($num)
{
    if ($num < 0)
        return false;
 
    // Considering the equation
    // n*(n+1)/2 = num
    // The equation is :
    // a(n^2) + bn + c = 0";
    $c = (-2 * $num);
    $b = 1; $a = 1;
    $d = ($b * $b) - (4 * $a * $c);
 
    if ($d < 0)
        return false;
 
    // Find roots of equation
    $root1 = (-$b + (float)sqrt($d)) / (2 * $a);
         
    $root2 = (-$b - (float)sqrt($d)) / (2 * $a);
 
    // checking if root1 is natural
    if ($root1 > 0 && floor($root1) == $root1)
        return true;
 
    // checking if root2 is natural
    if ($root2 > 0 && floor($root2) == $root2)
        return true;
 
    return false;
}
 
// Driver code
$num = 55;
if (isTriangular($num))
    echo("The number is" .
         " a triangular number");
else
    echo ("The number " .
          "is NOT a triangular number");
 
// This code is contributed
// by Code_Mech.
?>

Javascript




<script>
// javascript program to check if a number is a
// triangular number using quadratic equation.
 
    // Returns true if num is triangular
    function isTriangular(num)
    {
        if (num < 0)
            return false;
 
        // Considering the equation
        // n*(n+1)/2 = num
        // The equation is :
        // a(n^2) + bn + c = 0";
        var c = (-2 * num);
        var b = 1, a = 1;
        var d = (b * b) - (4 * a * c);
 
        if (d < 0)
            return false;
 
        // Find roots of equation
        var root1 = (-b +  Math.sqrt(d)) / (2 * a);
 
        var root2 = (-b -  Math.sqrt(d)) / (2 * a);
 
        // checking if root1 is natural
        if (root1 > 0 && Math.floor(root1) == root1)
            return true;
 
        // checking if root2 is natural
        if (root2 > 0 && Math.floor(root2) == root2)
            return true;
 
        return false;
    }
 
    // Driver code
     
    var num = 55;
    if (isTriangular(num))
        document.write("The number is" + " a triangular number");
    else
        document.write("The number " + "is NOT a triangular number");
 
// This code is contributed by Rajput-Ji
</script>

Output: 

 The number is a triangular number 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :