Rectangular Number in JavaIn 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 NumberA 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 ExampleThe 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. 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.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. 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 n^{th} rectangular number by using the following formula:
R_{n} = n(n + 1) or n^{2} + n
Let's find the 4^{th} rectangular number. Given number is 4. R_{n }= n (n + 1) R_{n }= n^{2} + n R_{4 }= (4)^{2 }+ 4 R_{4 }= 20 Hence, the 4^{th} rectangular number is 20. Steps to Find Rectangular Number
Let's implement the above steps in a Java program. Rectangular Number Java ProgramConsider the following example to check whether given number is a rectangular number or not. RectangularNumberTest.java Output: Find n^{th} Rectangular NumberConsider following example where we print the n^{th} rectangular number, the value of n is provided by the user. RectangularNumberExample.java Output: |