JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript 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 JavaScript program to check if a given number is a triangular number.
π Example
Let's take a look at the JavaScript code that checks whether a given number is a triangular number.
// Function to check if a number is a triangular number
function isTriangularNumber(num) {
// Formula to check triangular number
const n = Math.sqrt(2 * num);
return Math.floor(n) * (Math.floor(n) + 1) / 2 === num;
}
// Driver program
// Replace this value with your desired number
const number = 10;
// Check if the number is a triangular number
if (isTriangularNumber(number)) {
console.log(`${number} is a triangular number.`);
} else {
console.log(`${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 isTriangularNumber that checks if a given number is a triangular number using the formula.
- Replace the value of number in the main program with the desired number to check.
- The program then calls the isTriangularNumber function and logs whether the number is a triangular number or not.
π Between the Given Range
Let's take a look at the javascript code that checks and displays triangular numbers in the range of 1 to 50.
// Function to check if a number is triangular
function isTriangular(num) {
// Formula to check if a number is triangular
return Math.sqrt(8 * num + 1) % 1 === 0;
}
// Function to find and display triangular numbers in the range 1 to 50
function findTriangularNumbers() {
console.log("Triangular Numbers in the Range 1 to 50:");
for (let i = 1; i <= 50; i++) {
if (isTriangular(i)) {
process.stdout.write(i + " ");
}
}
console.log(); // Move to the next line for better readability
}
// Call the function to find and display triangular numbers
findTriangularNumbers();
π» 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 whether a given number is a triangular number using the triangular number formula.
- It defines a function findTriangularNumbers that iterates through numbers from 1 to 50 and prints those that are triangular.
- The main section calls the function to find and display triangular numbers in the specified range.
π§ 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 (JavaScript Program to Check Triangular Number), please comment here. I will help you immediately.