×

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview questions.

Meaning

Rectangular numbers are known by other names such as -Pronic Number, oblong number, rectangular number, or heteromecic number.

A rectangular number is a special numberthat is the product of two consecutive integers, that is, n and (n + 1). The product can be expressed as n*(n+1)

It can also be understood that rectangular numbers are those numbers that can be arranged to form a rectangle.

Examples:

Some of the initial pronic numbers include: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462…..

2 is considered the pronic number because it is the product of 2 consecutive numbers.

2 = 1 * 2      

It is forming a rectangle of 1 row and 2 columns.

Similarly, 6 is considered the pronic number because it is the product of 2 consecutive numbers.

6 = 2 * 3

It is forming rectangle of 2 row and 3 columns.

Similarly, 12 is considered the pronic number because it is the product of 2 consecutive numbers.

12 = 3 * 4

It is forming a rectangle of 3rows and 4 columns.

Similarly, 30 is considered the pronic number because it is the product of 2 consecutive numbers.

30 = 5*6

Here, it is forming a rectangle of 5rows and 6 columns.

Similarly, 56 is considered the pronic number because it is the product of 2 consecutive numbers.

56 = 7 * 8

It is forming a rectangle of 7 rows and 8 columns.

42 is considered the pronic number because it is the product of 2 consecutive numbers.

42 = 6 * 7

It is forming a rectangle of 6rows and 7 columns.

72 is considered the pronic number because it is the product of 2 consecutive numbers.

72 = 8 * 9

It is forming a rectangle of 8rows and 9 columns.

0 is considered the pronic number because it is the product of 2 consecutive numbers.

0 = 0 * 1

Numbers those are not rectangular

3 is not considered as a rectangular number because it is not the product of 2 consecutive numbers.

3 = 3 * 1

Here, 1 row and 3 columns are being formed. Thus, the criteria for being a rectangular number get failed. So, it is not a rectangular number.

5 is not considered as a rectangular number because it is not the product of 2 consecutive numbers.

5 = 5 * 1

In the same way, here 1 row and 5 columns are being formed, violating the criteria of a rectangular number. So, it is also not a rectangular number.

7 is not considered as a rectangular number because it is not the product of 2 consecutive numbers.

7 = 7 * 1

In the same way, here 1 row and 7 columns are being formed, violating the criteria of a rectangular number. So, it is also not a rectangular number.

9 is also not considered as a rectangular number because it does not form a rectangle with consecutive numbers of rows and columns.

Implementation

Now, we will see a java code to understand the meaning of the rectangular number in a more practical way.

Code

//A program to check whether an entered no. is pronic or not


import java.util.*;
class RectangularNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);


System.out.print("Enter any number : ");
        int num = sc.nextInt();
        int flag = 0;


        for(int j=0; j<num; j++)
        {
            if(j*(j+1) == num)
            {
                flag = 1;
                break;
            }
        }


        if(flag == 1)
System.out.println(num+" is a Rectangular Number.");
        else
System.out.println(num+" is not a Rectangular Number.");      
    }
}

Outputs:

1.

Rectangular Numbers in Java

Explanation: The no. 87 is checked and is found that it is not a rectangular number.

2.

Rectangular Numbers in Java

Explanation: The no. 6 is checked and is found that it is a rectangular number

Summary

In this tutorial, we tried to understand what is meant by rectangular number, saw a good number of examples, and further saw its implementation in java.


Related Topics

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

Majority Element in Java

It's an extremely intriguing question that is commonly asked in job interviews at prestigious IT firms like The Google, Amazon, TCS, and The Accenture, etc. By figuring out the solution, one may...

10 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.