
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Check Whether a Number is a Triangular Number or Not in Java?
A Triangular number of a natural number n, which is defined as the sum of all natural numbers starting from 1 to n.
It is called triangular as we can represent it in the form of an equilateral triangular grid by using the total number of points where the row number equals the point number, which means in the 1st row 1 point, in the 2nd row 2 points, in the 3rd row 3 points, and so on. Examples of triangular numbers are 1, 3, 6, 10, 21, 55, etc.
In simple terms, we can say a number is a triangular number if it is the sum of all consecutive numbers starting from 1. To find the triangular number, we have a formula as follows:
formula = n x (n + 1)/2
Where n is a natural number (e.g., 1, 2, 3, 4,... so on), the result is called a triangular number. It represents the sum of the first n natural numbers.
Input & Output Scenarios
The following are a few input and output scenarios that help you to check whether the number is a triangular number using the above formula:
Scenario 1
Suppose the given number is 10:
Input: 10 Output: Yes Calculation: Let's take n = 4 to check, result = n x (n + 1)/2 = 4 x (4 + 1)/2 = 20/2 = 10
Since result == 10, the 10 is a triangular number.
Scenario 2
Suppose the input number is 7:
Input: 7 Output: No Calculation: Let's take n = 3, result = n x (n + 1)/2 = 3 x (3 + 1)/2 = 12/2 = 6
Since result != 7, 7 is not a triangular number.
Example 1
The following example checks whether the given number 6 is a triangular number by comparing the sum of all consecutive numbers starting from 1 with the number itself. If the sum becomes equal to the number, the number is a triangular number:
public class triangularNumber{ public static void main(String args[]){ int num = 6; System.out.println("The given number is: " + num ); int sum = 0; boolean activeFlag = false; for(int i = 1; sum<=num ; i++){ sum += i; if(sum == num ){ activeFlag = true; break; } } if(activeFlag){ System.out.println("Yes! " + num +" is a triangular number"); } else{ System.out.println("No! " + num + "is not a triangular number"); } } }
The above program produces the following output:
The given number is: 6 Yes! 6 is a triangular number
Example 2
In the example below, we define a method named checkTriangular(), which calculates the sum of all consecutive numbers and compares the sum with the number 56 itself; if it is equal, then the number is a triangular number:
public class triangularNumber { static boolean checkTriangular(int num){ int sum = 0; for(int i = 1; sum<=num; i++){ sum = sum + i; if(sum == num){ return true; } } return false; } public static void main(String args[]){ int num = 56; System.out.println("Given number: " + num); boolean result = checkTriangular(num); if(result){ System.out.println("Yes! " + num + " is a triangular number."); } else{ System.out.println("No! " + num + " is not a triangular number."); } } }
Following is the output of the above program:
Given number: 56 No! 56 is not a triangular number.
Example 3
This is another way of checking triangular numbers using the formula n * (n + 1) / 2. We will compare the result with the original number; if they are equal, the number is triangular; otherwise, it is not:
public class TriangularNumberCheck { public static boolean isTriangular(int num) { int n = (int) Math.sqrt(2 * num); boolean res = n * (n + 1) / 2 == num; if(res){ return true; } return false; } public static void main(String[] args) { int num = 10; System.out.println("The given number is: " + num); System.out.println(num + " is triangular: " + isTriangular(num)); } }
Following is the output of the above program:
The given number is: 10 10 is triangular: true