Java Program to Check Triangular Number

In the previous article we have discussed Java Program to Check Hamming Number

In this article we are going to understand what Triangular number is and how we can check whether a number is Triangular or not in Java with examples.

Program to Check Triangular Number

Triangular numbers are numbers formed by addition of consecutive numbers starting from 1.

 Example :

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

In the above examples the numbers are Triangular numbers as they are sum of consecutive numbers from 1.

Let’s see different ways to check triangular number.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach :

  1. Enter a number/declare a number and store it .
  2. We try adding the numbers from 1 to the number until we find an equivalent match.
  3. If there is an equivalent match found, then the number is said to be Triangular number.

Method-1: By Using Static Value

import java.util.Scanner;
public class TriangularNumber
{
    public static void main(String args[])
    {
        //A number declared
        int num = 21;

        if(isTriangularNumber(num))
        {
            System.out.println(num+" is a Triangular number");
        }
        else
        {
            System.out.println(num+" is Not a Triangular number");
        }
    }

    // Function that checks whether a number is Triangular or not
    static boolean isTriangularNumber(int num)
    {
        // Sum is initialized to 1 and not 0 as our loop starts from 2 directly
        int sum = 1;
        boolean flag = false;
        // Loop that adds consecutive digits
        for(int i = 2; i<num; i++)
        {
            sum = sum + i;
            if(sum == num)
            {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
Output:

21 is a Triangular number

Method-2: By User Defined Method

import java.util.Scanner;
public class TriangularNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        if(isTriangularNumber(num))
        {
            System.out.println(num+" is a Triangular number");
        }
        else
        {
            System.out.println(num+" is Not a Triangular number");
        }
    }

    // Function that checks whether a number is Triangular or not
    static boolean isTriangularNumber(int num)
    {
        // Sum is initialized to 1 and not 0 as our loop starts from 2 directly
        int sum = 1;
        boolean flag = false;
        // Loop that adds consecutive digits
        for(int i = 2; i<num; i++)
        {
            sum = sum + i;
            if(sum == num)
            {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
Output:

Case-1

Enter a number : 21
21 is a Triangular number


Case-1

Enter a number : 22
22 is a Not Triangular number

Method-3: C++ Program to Check Triangular Number

#include <iostream>
using namespace std;
int main()
{
  int num,flag=0,sum=0;
  cout<<"Enter a number: ";
  cin>>num;
  int c=1;
  while(sum<num)
  {
    sum=0;
    for(int i=1;i<=c;++i)
    {
      sum+=i;
    }
    if(sum==num) {flag=1;break;}
    c++;
  }
  if(flag) cout<<"Triangle number"<<endl;
  else cout<<"Not Triangle number "<<endl;
  return 0;
}
Output:

Enter a number: 21
Triangular Number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: