Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Check Triangular Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, various mathematical properties of numbers often pique our interest. One such property is whether a number is a triangular number.
A triangular number or triangle number is the sum of the natural numbers up to a certain value.
In this tutorial, we'll delve into a Python program to check if a given number is a triangular number.
π Example
Let's explore the Python code that checks whether a given number is a triangular number.
import math
# Function to check if a number is a triangular number
def is_triangular_number(num):
# Formula to check triangular number
n = int(math.sqrt(2 * num))
return n * (n + 1) // 2 == num
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
number = 10
# Check if the number is a triangular number
if is_triangular_number(number):
print(f"{number} is a triangular number.")
else:
print(f"{number} is not a triangular number.")
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the code.
10 is a triangular number.
Run the script to check if the given number is a triangular number.
π§ How the Program Works
- The program defines a function is_triangular_number that checks if a given number is a triangular number using the formula.
- Inside the if __name__ == "__main__": block, replace the value of number with the desired number to check.
- The program then calls the is_triangular_number function and prints whether the number is a triangular number or not.
π Between the Given Range
Let's take a look at the python code that checks and displays triangular numbers in the range of 1 to 50.
# Function to check if a number is triangular
def is_triangular(num):
n = 1
triangular_number = 1
while triangular_number < num:
n += 1
triangular_number = n * (n + 1) // 2
return triangular_number == num
# Display triangular numbers in the range 1 to 50
def display_triangular_numbers():
lower_limit = 1
upper_limit = 50
print(f"Triangular Numbers in the Range {lower_limit} to {upper_limit}:")
for num in range(lower_limit, upper_limit + 1):
if is_triangular(num):
print(num, end=" ")
# Call the function to display triangular numbers
display_triangular_numbers()
π» Testing the Program
Triangular Numbers in the Range 1 to 50: 1 3 6 10 15 21 28 36 45
Run the script to see the triangular numbers in the specified range.
π§ How the Program Works
- The program defines a function is_triangular that checks if a given number is a triangular number.
- Inside the function, it uses a while loop to find the smallest triangular number greater than or equal to the given number.
- The display_triangular_numbers function iterates through the range 1 to 50, calling is_triangular for each number and printing the triangular numbers.
π§ Understanding the Concept of Triangular Numbers
Before delving into the code, let's take a moment to understand the concept of triangular numbers.
A triangular number is the sum of the natural numbers up to a certain value, and it can be represented by the formula Tn = n * (n + 1) / 2, where n is a non-negative integer.
For example, T3 = 1 + 2 + 3 = 6 is a triangular number.
π’ Optimizing the Program
While the provided program is effective, you can explore and implement optimizations based on the properties of triangular numbers.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Triangular Number), please comment here. I will help you immediately.