Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

JavaScript Program to check Perfect Square

Updated on Jan 11, 2024
By Mari Selvan
πŸ‘οΈ 102 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
JavaScript Program to check Perfect Square

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, various mathematical problems challenge programmers to devise efficient solutions. One such problem is determining whether a given number is a perfect square.

A perfect square is a number that is the square of an integer, meaning it can be expressed as the product of an integer multiplied by itself.

In this tutorial, we'll explore a JavaScript program designed to check whether a given number is a perfect square or not.

πŸ“„ Example

Let's take a look at the JavaScript code that accomplishes this task.

isPerfectSquare.js
Copied
Copy To Clipboard
// Function to check if a number is a perfect square
function isPerfectSquare(number) {
  for (let i = 1; i * i <= number; i++) {
    // If i * i is equal to the number, it's a perfect square
    if (i * i === number) {
      return true;
    }
  }

  return false;
}

// Driver program
// Replace this value with your desired number
const testNumber = 16;

// Check if the number is a perfect square
if (isPerfectSquare(testNumber)) {
  console.log(`${testNumber} is a perfect square.`);
} else {
  console.log(`${testNumber} is not a perfect square.`);
}

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of testNumber in the code.

Output
16 is a perfect square.

Run the script to see if the number is a perfect square.

🧠 How the Program Works

  1. The program defines a function isPerfectSquare that takes a number as input and returns true if the number is a perfect square, and false otherwise.
  2. Inside the function, it iterates through integers starting from 1 until the square of the current integer is greater than the given number.
  3. If the square of the current integer is equal to the given number, the function returns true indicating a perfect square.
  4. The main program then checks if a specific number (in this case, 16) is a perfect square and logs the result.

πŸ“ Between the Given Range

Let's take a look at the javascript code that checks for perfect squares in the specified range.

findPerfectSquaresInRange.js
Copied
Copy To Clipboard
// Function to check if a number is a perfect square
function isPerfectSquare(number) {
  let sqrt = Math.sqrt(number);
  return sqrt - Math.floor(sqrt) === 0;
}

// Driver program
function findPerfectSquaresInRange(start, end) {
  let perfectSquares = [];

  for (let i = start; i <= end; i++) {
    if (isPerfectSquare(i) && i !== 1) {
      perfectSquares.push(i);
    }
  }

  return perfectSquares;
}

// Define the range
const startRange = 1;
const endRange = 50;

// Call the function to find perfect squares
const perfectSquaresInRange = findPerfectSquaresInRange(startRange, endRange);

// Display the result
console.log(`Perfect Squares in the range ${startRange} to ${endRange}:`);
console.log(perfectSquaresInRange.join(' '));

πŸ’» Testing the Program

The provided program checks for perfect squares in the range from 1 to 50. You can modify the range by changing the values of startRange and endRange in the code.

Output
Perfect Squares in the Range 1 to 50:
1 4 9 16 25 36 49 

Run the script to see the perfect squares in the specified range.

🧠 How the Program Works

  1. The program defines a function isPerfectSquare that checks if a given number is a perfect square.
  2. Another function findPerfectSquaresInRange iterates through the specified range and identifies perfect squares.
  3. The main part of the program calls these functions and displays the result in the specified output format.

🧐 Understanding the Concept of Perfect Square

Before diving into the code, let's take a moment to understand the concept of perfect squares.

A perfect square is a number that can be expressed as the product of an integer multiplied by itself.

For example, 4, 9, and 16 are perfect squares because they are the squares of 2, 3, and 4, respectively.

🎒 Optimizing the Program

While the provided program is effective, there are alternative methods for checking perfect squares that involve mathematical properties. Consider exploring and implementing such optimizations to enhance the efficiency of your program.

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
8 months ago

If you have any doubts regarding this article (JavaScript Program to check Perfect Square), 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