Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java Program to Check Pronic Number

Posted in Java Tutorial
Updated on Oct 30, 2024
By Mari Selvan
πŸ‘οΈ 59 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Java Program to Check Pronic Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, mathematical patterns and properties often become interesting challenges. One such intriguing concept is the "Pronic Number."

A pronic number, also known as a rectangular number, oblong number, or heteromecic number, is the product of two consecutive integers.

In this tutorial, we will delve into a Java program designed to check whether a given number is a pronic number or not.

πŸ“„ Example

Now, let's delve into the Java code that checks whether a given number is a pronic number or not.

PronicNumberChecker.java
Copied
Copy To Clipboard
public class PronicNumberChecker {

  // Function to check if a number is a pronic number
  static boolean isPronic(int num) {
    int sqrtNum = (int) Math.sqrt(num);

    return (sqrtNum * (sqrtNum + 1) == num);
  }

  // Driver program
  public static void main(String[] args) {
    // Replace this value with your desired number
    int number = 12;

    // Check if the number is a pronic number
    if (isPronic(number)) {
      System.out.println(number + " is a pronic number.");
    } else {
      System.out.println(number + " is not a pronic number.");
    }
  }
}

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of number in the main method.

Output
12 is a pronic number.

Compile and run the program to check if the given number is a pronic number.

🧠 How the Program Works

  1. The program defines a class PronicNumberChecker containing a static method isPronic that takes a number as input and returns true if the number is a pronic number and false otherwise.
  2. Inside the method, it calculates the square root of the input number.
  3. It checks whether the product of the square root and the square root incremented by 1 equals the input number. If yes, the number is a pronic number.
  4. The driver program tests the function with a sample number and prints the result.

πŸ“ Between the Given Range

Let's take a look at the java code that checks for pronic numbers in the range from 1 to 20.

PronicNumbers.java
Copied
Copy To Clipboard
public class PronicNumbers {

  // Function to check if a number is Pronic
  static boolean isPronic(int num) {
    int sqrt = (int) Math.sqrt(num);
    return (sqrt * (sqrt + 1) == num);
  }

  // Driver program
  public static void main(String[] args) {
    System.out.print("Pronic Numbers in the range 1 to 20:\n");

    // Check and display Pronic Numbers in the range 1 to 20
    for (int i = 1; i <= 20; i++) {
      if (isPronic(i)) {
        System.out.print(i + " ");
      }
    }
  }
}

πŸ’» Testing the Program

Output
Pronic Numbers in the range 1 to 20:
2 6 12 20

Compile and run the program to see the Pronic Numbers in the specified range.

🧠 How the Program Works

  1. The program defines a class PronicNumbers containing a static method isPronic to check if a given number is Pronic.
  2. Inside the method, it calculates the square root of the number and checks if it satisfies the Pronic condition (nΓ—(n+1)==num).
  3. The main function iterates through the range from 1 to 20 and prints the Pronic Numbers.

🧐 Understanding the Concept of Pronic Numbers

Before delving into the code, let's take a moment to understand the concept of pronic numbers.

A pronic number is a product of two consecutive integers, and it has applications in various mathematical and geometric contexts.

A pronic number, denoted by Pn, is a number of the form Pn=n Γ— (n+1) where n is a non-negative integer. The sequence of pronic numbers starts as 0, 2, 6, 12, 20, and so on.

For example, let's take n = 3. The corresponding pronic number would be P3 = 3x(3+1) = 12.

🎒 Optimizing the Program

While the provided program is effective, consider exploring ways to optimize the computation of pronic numbers for larger inputs.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (Java Program to Check Pronic Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy