JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript 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'll delve into a JavaScript program designed to check whether a given number is a pronic number or not.
π Example
Now, let's dive into the JavaScript code that checks whether a given number is a pronic number or not.
// Function to check if a number is a pronic number
function isPronic(num) {
for (let i = 0; i <= Math.sqrt(num); i++) {
if (i * (i + 1) === num) {
return true;
}
}
return false;
}
// Driver program
// Replace this value with your desired number
const number = 12;
// Check if the number is a pronic number
if (isPronic(number)) {
console.log(`${number} is a pronic number.`);
} else {
console.log(`${number} is not a pronic number.`);
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the code.
12 is a pronic number.
Run the script to check if the given number is a pronic number.
π§ How the Program Works
- The program defines a function isPronic that takes a number as input and returns true if the number is a pronic number and false otherwise.
- Inside the function, it calculates the square root of the input number.
- 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.
- The driver program tests the function with a sample number and logs the result.
π Between the Given Range
Let's take a look at the javascript code that checks for pronic numbers in the range from 1 to 20.
// Function to check if a number is a Pronic Number
function isPronicNumber(num) {
for (let i = 0; i <= Math.sqrt(num); i++) {
if (i * (i + 1) === num) {
return true;
}
}
return false;
}
// Function to find and display Pronic Numbers in a specified range
function findPronicNumbersInRange(start, end) {
let pronicNumbers = [];
for (let num = start; num <= end; num++) {
if (isPronicNumber(num)) {
pronicNumbers.push(num);
}
}
return pronicNumbers.join(' ');
}
// Driver program
// Specifying the range from 1 to 20
const startRange = 1;
const endRange = 20;
// Finding Pronic Numbers in the specified range
const pronicNumbersInRange = findPronicNumbersInRange(startRange, endRange);
console.log(`Pronic Numbers in the range ${startRange} to ${endRange}:`);
console.log(pronicNumbersInRange);
π» Testing the Program
Pronic Numbers in the range 1 to 20: 2 6 12 20
Run the script to see the Pronic Numbers in the range from 1 to 20.
π§ How the Program Works
- The program defines a function isPronicNumber that checks whether a given number is a Pronic Number.
- The findPronicNumbersInRange function iterates through the specified range and collects Pronic Numbers.
- The driver program specifies the range from 1 to 20 and prints the Pronic Numbers found in that range.
π§ 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:
Author
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
If you have any doubts regarding this article (JavaScript Program to Check Pronic Number), please comment here. I will help you immediately.