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.
Below is the implementation of above approach
// 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 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.
// 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.
<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>
<?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
?>
# 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.
Output
The number is a triangular number
Time Complexity: O(N)
Auxiliary Space: O(1)
Triangular Numbers
A number is termed a triangular number if we can represent it in the form of a 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).
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.