C++ Program to find Perfect Squares in an Array.

The following programs will print all perfect square values in an array. 

Perfect Square Image.

C++ Program to find Perfect Squares in an Array:

A perfect square number is a number that is equal to the square of a number. For example:
4 is a perfect square of 2 i.e. 2*2 = 4.
There are two ways we can do this. Both are given below.

First Way:

Steps:

  • Find the Square root of the number under observation.
  • Multiply the answer of the square root with itself. If the result is equal to the original number under observation, then the original number is a perfect square.

Source Code:

#include <iostream>
#include <math.h>

using namespace std;

bool isPerfectSquare(int a)
{
    if (a == 1)
    {
        return false;
    }
    int b = sqrt(a);
    if ((b * b) == a)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    int sizeOfArray = 20;
    int arr[sizeOfArray];

    for (int i = 0count = 1i < sizeOfArrayi++, count++)
    {
        arr[i] = count;
    }

    cout << "Original Array: " << endl;
    for (int i = 0i < sizeOfArrayi++)
    {
        cout << arr[i<< " ";
    }
    cout << endl;

    // printing complete squares.
    cout << "Complete square numbers in the array are: " << endl;
    bool foundPerfectSquare = false;
    bool eachElementCheck;

    for (int i = 0i < sizeOfArrayi++)
    {
        eachElementCheck = isPerfectSquare(arr[i]);
        if (eachElementCheck == true)
        {
            cout << arr[i<< " ";
            foundPerfectSquare = true;
        }
    }
    if (foundPerfectSquare == false)
    {
        cout << "There are no complete square numbers in the Array.";
    }
    cout << endl;

    return 0;
}

Output:

Original Array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Complete square numbers in the array are:
4 9 16

Second Way:

Steps:

  • Take the Square of every natural number (starting from 2).
  • Compare the square with the number under observation. If both are equal then the number is a perfect square and vice versa.
One more thing, 1/3rd of a number is the maximum value up to which we compare squares. It is because, after this value, the value of the square will become greater than the number under observation.

Source code:

#include <iostream>

using namespace std;

int main()
{
    int sizeOfArray = 20;
    int arr[sizeOfArray];

    for (int i = 0count = 1i < sizeOfArrayi++, count++)
    {
        arr[i] = count;
    }

    cout << "Original Array: " << endl;
    for (int i = 0i < sizeOfArrayi++)
    {
        cout << arr[i<< " ";
    }
    cout << endl;

    // printing complete squares.
    cout << "Complete square numbers in the array are: " << endl;
    bool foundPerfectSquare = false;

    for (int i = 0i < sizeOfArrayi++)
    {
        for (int j = 2j <= (arr[i] / 3) + 1j++)
        {
            if ((j * j) == arr[i])
            {
                cout << arr[i<< " ";
                foundPerfectSquare = true;
                break;
            }
        }
    }
    if (foundPerfectSquare == false)
    {
        cout << "There are no complete square numbers in the Array.";
    }
    cout << endl;

    return 0;
}

Output:

Original Array: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Complete square numbers in the array are:
4 9 16

Remarks:

Both the programs have the same output. But program 1 takes fewer steps to identify if a number is a perfect square or not.

If you have any queries about this program or about anything, do ask in a comment. I will try to answer as many questions as I can.

Comments