How to Check Whether a Number is a Triangular Number or Not in Java?


Triangular number of a natural ‘n’ can be defined as the sum of all natural numbers from 1 to n.

It is called as 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. Means in 1st row 1 point, in 2nd row 2 points, in 3rd row 3 points and so on.

In simple terms we can say a number is a triangular number if it is the sum of all consecutive numbers starting from 1.

Mathematically n*(n+1)/2

In this article we will see how to check if a number is triangular by using Java programming language.

To show you some instances

Instance-1

Input number is 10

Let’s check it by using the logic of triangular number −

1 + 2 + 3 + 4 = 10 which is the sum of all consecutive numbers starting from 1.

Hence, 10 is a triangular number

Instance-2

Input number is 15

Let’s check it by using the logic of triangular number −

1 + 2 + 3 + 4 + 5 = 15 which is the sum of all consecutive numbers starting from 1.

Hence, 15 is a triangular number.

Instance-3

Input number is 7

Let’s check it by using the logic of triangular number −

1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10

So, from above we got to know 7 is not a triangular number.

Some other examples of triangular numbers include 1, 3, 6, 21, 55 etc.

Algorithm

  • Step 1 − Get an integer number either by initialization or by user input.

  • Step 2 − Take a loop and iterate from 1 to the input number. And keep on adding each number and simultaneously check if at any point the sum is equal to the original input number.

  • Step 3 − If at any point sum is equal to original input number then break the loop and print it is a triangular number else print it is not a triangular number.

Multiple Approaches

We have provided the solution in 3 different approaches.

  • By Using Static Input Value

  • By Using User Defined Method

  • Find Triangular Number of a Number

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, an integer value will be initialized in the program and then by using the algorithm we can check whether a number is a triangular number or not.

Example

public class Main{ public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 6; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //declaring an integer variable sum to hold the sum value //initializing it with value as 0 int sum = 0; //declare boolean variable activeFlag as false boolean activeFlag = false; // Loop that adds consecutive digits for(int i = 1; i<=originalNumber; i++){ sum = sum + i; //if sum is equal with original number //then make the activeFlag as true; if(sum == originalNumber){ activeFlag = true; break; } } //if activeFlag is true then given number is triangular number //print it is triangular number if(activeFlag == true) System.out.println(originalNumber+" is a triangular number"); //else print given number is not a triangular number else { System.out.println(originalNumber+" is not a triangular number"); } } }

Output

Given number: 6
6 is a triangular number

Approach-2: By Using User Defined Method

In this approach, an integer value will be initialized in the program and then we will call a user defined method by passing this given number as parameter.

Inside the method we will check whether a number is a triangular number or not by using the algorithm.

Example

public class Main{ public static void main(String args[]){ //Declared an integer variable and initialized a number as value int originalNumber = 55; //printing the given number to be checked System.out.println("Given number: "+originalNumber); //call the user defined method to check a number is triangular or not //by passing the input number as parameter if(checkTriangular(originalNumber)){ System.out.println(originalNumber+" is a triangular number"); } else { System.out.println(originalNumber+" is not a triangular number"); } } //user defined method to check triangular number static boolean checkTriangular(int originalNumber){ //declaring an integer variable sum to hold the sum value //initializing it with value as 0 int sum = 0; // Loop that adds consecutive digits for(int i = 1; i<=originalNumber; i++){ sum = sum + i; if(sum == originalNumber){ return true; } } return false; } }

Output

Given number: 55
55 is a triangular number

Approach-3: Find Triangular Number of a Number

In this we will find a triangular number of a given number. Means a number is given say ‘n’ and we have to find n*(n+1)/2

Example

public class Main{ //main method public static void main(String[] args){ //declared and initialized an integer variable int inputNumber = 7; //declared an integer variable and initialize with value 0 int triangularNumber = 0; //using loop we will find the sum of consecutive numbers from 1 to that number for(int i = 1 ; i<=inputNumber ; i++){ triangularNumber = triangularNumber + i; } System.out.println(triangularNumber+" is the triangular number for "+inputNumber); } }

Output

28 is the triangular number for 7

In this article, we explored how to check a number whether it is a triangular number or not in Java by using different approaches.

Updated on: 28-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements