PHP Basic
PHP Interview Programs
- PHP Interview Programs
- PHP Abundant Number
- PHP Amicable Number
- PHP Armstrong Number
- PHP Average of N Numbers
- PHP Automorphic Number
- PHP Biggest of three numbers
- PHP Binary to Decimal
- PHP Common Divisors
- PHP Composite Number
- PHP Condense a Number
- PHP Cube Number
- PHP Decimal to Binary
- PHP Decimal to Octal
- PHP Disarium Number
- PHP Even Number
- PHP Evil Number
- PHP Factorial of a Number
- PHP Fibonacci Series
- PHP GCD
- PHP Happy Number
- PHP Harshad Number
- PHP LCM
- PHP Leap Year
- PHP Magic Number
- PHP Matrix Addition
- PHP Matrix Division
- PHP Matrix Multiplication
- PHP Matrix Subtraction
- PHP Matrix Transpose
- PHP Maximum Value of an Array
- PHP Minimum Value of an Array
- PHP Multiplication Table
- PHP Natural Number
- PHP Number Combination
- PHP Odd Number
- PHP Palindrome Number
- PHP Pascalβs Triangle
- PHP Perfect Number
- PHP Perfect Square
- PHP Power of 2
- PHP Power of 3
- PHP Pronic Number
- PHP Prime Factor
- PHP Prime Number
- PHP Smith Number
- PHP Strong Number
- PHP Sum of Array
- PHP Sum of Digits
- PHP Swap Two Numbers
- PHP Triangular Number
PHP 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 PHP program to check if a given number is a triangular number.
π Example
Let's delve into the PHP code that checks whether a given number is a triangular number.
<?php
// Function to check if a number is a triangular number
function isTriangularNumber($num)
{
// Formula to check triangular number
$n = (int)sqrt(2 * $num);
return ($n * ($n + 1)) / 2 == $num;
}
// Replace this value with your desired number
$number = 10;
// Check if the number is a triangular number
if (isTriangularNumber($number))
{
echo "$number is a triangular number.\n";
}
else
{
echo "$number is not a triangular number.\n";
}
?>
π» 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 isTriangularNumber that checks if a given number is a triangular number using the formula.
- Replace the value of $number with the desired number to check.
- The program then calls the isTriangularNumber function and prints whether the number is a triangular number or not.
π Between the Given Range
Let's take a look at the php code that checks and displays triangular numbers in the range of 1 to 50.
<?php
// Function to check if a number is triangular
function isTriangular($num)
{
$sum = 0;
$i = 1;
// Keep adding natural numbers to the sum until it exceeds or equals $num
while ($sum < $num)
{
$sum += $i;
$i++;
}
// Check if the sum equals $num
return $sum == $num;
}
// Display triangular numbers in the range 1 to 50
echo "Triangular Numbers in the Range 1 to 50:\n";
for ($i = 1;$i <= 50;$i++)
{
if (isTriangular($i))
{
echo "$i ";
}
}
?>
π» 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 range from 1 to 50.
π§ How the Program Works
- The program defines a function isTriangular that checks if a given number is triangular using the triangular number formula.
- Inside the main section, a loop iterates through numbers from 1 to 50.
- For each number, the program checks if it is triangular using the isTriangular function.
- Triangular numbers in the specified range are displayed in the desired format.
π§ 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 (PHP Program to Check Triangular Number), please comment here. I will help you immediately.