×

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

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Java Boolean parseBoolean() Method

The parseBoolean() method of Boolean class returns a Boolean value for the specified String argument. It returns true if and only if string’s value is equal to “true”, else it...

1 minute read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.