Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A Pronic number (also known as a Heteromecic number) is a number that is the product of two consecutive integers, i.e., n * (n + 1). Examples of Pronic numbers include 0, 2, 6, 12, 20, etc. In Java, you can determine whether a number is Pronic by checking if it can be expressed as the product of two consecutive integers.
This article explains how to implement a Pronic number program in Java.
Check Pronic Number
To check whether a number is a Pronic number, we can follow these steps:
Start with an integer n.
Loop through numbers starting from 0 and check if i * (i + 1) == n.
If the condition is met, the number is Pronic; otherwise, it is not.
Java Program to Check Pronic Number
import java.util.Scanner;
public class PronicNumber {
public static boolean isPronic(int num) {
for (int i = 0; i <= Math.sqrt(num); i++) {
if (i * (i + 1) == num) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();
if (isPronic(number)) {
System.out.println(number + " is a Pronic number.");
} else {
System.out.println(number + " is NOT a Pronic number.");
}
}
}
You can also try this code with Online Java Compiler
public class PronicExample {
public static void main(String[] args) {
int[] numbers = {0, 2, 6, 12, 20, 30, 42, 56};
for (int num : numbers) {
if (isPronic(num)) {
System.out.println(num + " is a Pronic number.");
} else {
System.out.println(num + " is NOT a Pronic number.");
}
}
}
public static boolean isPronic(int num) {
for (int i = 0; i <= Math.sqrt(num); i++) {
if (i * (i + 1) == num) {
return true;
}
}
return false;
}
}
You can also try this code with Online Java Compiler
0 is a Pronic number.
2 is a Pronic number.
6 is a Pronic number.
12 is a Pronic number.
20 is a Pronic number.
30 is NOT a Pronic number.
42 is a Pronic number.
56 is a Pronic number.
Example 2: Checking Pronic Numbers in a Range
public class PronicRange {
public static void main(String[] args) {
System.out.println("Pronic numbers from 1 to 100: ");
for (int i = 0; i <= 10; i++) {
System.out.print((i * (i + 1)) + " ");
}
}
}
You can also try this code with Online Java Compiler
Pronic numbers from 1 to 100:
0 2 6 12 20 30 42 56 72 90
Time and Space Complexity
Time Complexity
The loop runs approximately √n times, making the time complexity O(√n).
If we check a number in a range, it will take O(N√N).
Space Complexity
We are using only a few variables, so the space complexity is O(1).
Frequently Asked Questions
What is a Pronic number?
A Pronic number is a number that is the product of two consecutive integers (n × (n + 1)).
How do you check if a number is Pronic in Java?
You can check by iterating from 0 to √n and verifying if i * (i + 1) == n.
What is the time complexity of checking a Pronic number?
The time complexity is O(√n) since we loop up to the square root of n.
Conclusion
In this article, we discussed on how to check if a number is a Pronic number in Java. A Pronic number (or rectangular number) is the product of two consecutive integers, represented as n × (n + 1). We implemented a Java program to determine if a given number is Pronic. Understanding this concept helps in improving mathematical logic and problem-solving in Java programming.