Javatpoint Logo
Javatpoint Logo

Rectangular Number in Java

In this section, we will learn what is a rectangular number and also create Java programs to check if the given number is a rectangular number or not. The rectangular number program is frequently asked in Java coding interviews and academics.

Rectangular Number

A rectangular number is a positive integer that can be arranged in rectangular forms. It is also called as pronic or oblong numbers.

In simple words, rectangular number is a special type of number that makes the shape of a rectangle using an array, where the elements are arranged based on rows and columns.

Rectangular Number Example

The number 2 is a rectangular number because it forms a rectangle of 1 row and 2 columns.

The number 6 is a rectangular number because it forms a rectangle of 2 row and 3 columns,

Similarly, the number 8 is a rectangular number because it forms a rectangle of 2 row and 4 columns.

Rectangular Number in Java

Note: The number 4 is the only even number that is not a rectangular number, because it forms square with array of 2 rows and 2 columns.

Rectangular Number in Java

Not only even, but some odd numbers can also form a rectangular shape. For example, the number 15 can form rectangle of 3 rows and 5 columns, as shown below.

Rectangular Number in Java

Some other rectangular numbers are:

0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420 …

We observe that we can calculate nth rectangular number by using the following formula:

Rn = n(n + 1) or n2 + n

Let's find the 4th rectangular number.

Given number is 4.

Rn = n (n + 1)

Rn = n2 + n

R4 = (4)2 + 4

R4 = 20

Hence, the 4th rectangular number is 20.

Steps to Find Rectangular Number

  • Read or initialize a number N.
  • Calculate the square of the given number N and store it in a variable square.
  • Add the number (N) itself in the square that we get from the previous step.
  • We get the rectangular number.

Let's implement the above steps in a Java program.

Rectangular Number Java Program

Consider the following example to check whether given number is a rectangular number or not.

RectangularNumberTest.java

Output:

Rectangular Number in Java

Find nth Rectangular Number

Consider following example where we print the nth rectangular number, the value of n is provided by the user.

RectangularNumberExample.java

Output:

Rectangular Number in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA