Python Program to Check Triangular Number


This Python program checks whether a given number by user is Triangular number or not.

Triangular Numbers are those numbers which are obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, ...

Triangular Number Example: 15 is Triangular Number because it can be obtained by 1+2+3+4+5+6 i.e. 1+2+3+4+5+6=15

List of Triangular Numbers: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666,

Also try: Check Triangular Number Online & Generate Triangular Numbers Online

Python Source Code: Triangular Number Check


# Python program to check Triangular Number

# Function to check Triangular

def is_triangular(n):

    if n==0 or n==1:
        return True
    
    triangular_sum = 0

    for i in range(n):
        triangular_sum += i

        if triangular_sum == n:
            return True

        if i == n:
            return False


# Reading number
number = int(input('Enter number: '))

# Making decision
if is_triangular(number):
    print('%d is TRIANGULAR.' %(number))
else:
    print('%d is NOT TRIANGULAR.' %(number))

Triangular Number Check Python Output

Run 1:
-----------------
Enter number: 15
15 is TRIANGULAR.

Run 2:
-----------------
Enter number: 696
696 is NOT TRIANGULAR.