Given an integer **N** print the sequence number of the given Triangular Number. If the number is not a triangular number then print -1.

A number is termed as 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, the second row has two points, the third row has three points and so on.

First 10 tringular number are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55.

**Examples:**

Input:N = 21

Output:6

Explanation:

Since 15 is a 6th Tringular Number.

Input:N = 12

Output:-1

Explanation:

Since 12 is not a tringular Number

**Approach:**

- Since tringular numbers are sum of natural numbers so can be generalise as quadratic equation.

(X*(X+1))/2 = N

X = (-1 + (1 + 8*N) ) /2

X = -0.5 + (0.25 + 2*N)^1/2

## C++

`// C++ code to print sequence ` `// number of a triangular number ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `main() ` `{ ` ` ` `int` `N = 21; ` ` ` `int` `A = ` `sqrt` `(2 * N + 0.25) - 0.5; ` ` ` `int` `B = A; ` ` ` ` ` `// If N is not tringular number ` ` ` `if` `(B != A) ` ` ` `cout << ` `"-1"` `; ` ` ` `else` ` ` `cout << B; ` `} ` ` ` `// This code is contributed by yatinagg ` |

*chevron_right*

*filter_none*

## Java

`// Java code to print sequence ` `// number of a triangular number ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `N = ` `21` `; ` ` ` `int` `A = (` `int` `)(Math.sqrt(` `2` `* N + ` `0.25` `) - ` `0.5` `); ` ` ` `int` `B = A; ` ` ` ` ` `// If N is not tringular number ` ` ` `if` `(B != A) ` ` ` `System.out.print(` `"-1"` `); ` ` ` `else` ` ` `System.out.print(B); ` `} ` `} ` ` ` `// This code is contributed by Akanksha_Rai ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to print sequence ` `# number of a triangular number ` ` ` `import` `math ` ` ` `N ` `=` `21` `A ` `=` `math.sqrt(` `2` `*` `N ` `+` `0.25` `)` `-` `0.5` `B ` `=` `int` `(A) ` ` ` `# if N is not tringular number ` `if` `B !` `=` `A: ` ` ` `print` `(` `-` `1` `) ` `else` `: ` ` ` `print` `(B) ` |

*chevron_right*

*filter_none*

## C#

`// C# code to print sequence ` `// number of a triangular number ` `using` `System; ` `class` `GFG{ ` ` ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 21; ` ` ` `int` `A = (` `int` `)(Math.Sqrt(2 * N + 0.25) - 0.5); ` ` ` `int` `B = A; ` ` ` ` ` `// If N is not tringular number ` ` ` `if` `(B != A) ` ` ` `Console.Write(` `"-1"` `); ` ` ` `else` ` ` `Console.Write(B); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity: **O(1)

**Auxiliary Space: **O(1)

## Recommended Posts:

- Program to find the Volume of a Triangular Prism
- First triangular number whose number of divisors exceeds N
- Program to print triangular number series till n
- Smallest triangular number larger than p
- Squared triangular number (Sum of cubes)
- Centered triangular number
- Check if a number can be represented as a sum of 2 triangular numbers
- Program to check if N is a Centered Triangular Number
- Index of smallest triangular number with N digits
- Triangular Numbers
- Sum of the series 1, 3, 6, 10... (Triangular Numbers)
- Maximum height of triangular arrangement of array values
- Program to calculate the Surface Area of a Triangular Prism
- Clockwise Triangular traversal of a Binary Tree
- Count of ways to travel a cyclic path in N steps in a Triangular Pyramid
- Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
- Convert an unbalanced bracket sequence to a balanced sequence
- Find Nth number in a sequence which is not a multiple of a given number
- Find if the given number is present in the infinite sequence or not
- Find a sequence of N prime numbers whose sum is a composite number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.