How to check if a number is a perfect square C++

Perfect Square:

In mathematics, a square number, sometimes also called a perfect square, is an integer that is the square of an integer;  in other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3?×?3. (Text from Wikipedia)

We are going to write a program that checks if the input number is a perfect square or not.
C program to check Perfect Square:

/** C Program to check Perfect Square **/ #include <stdio.h> int main() { int a, n; printf("Enter a number: "); scanf("%d", &n); for(a = 0; a <= n; a++) { if (n == a * a) { printf("YES"); return 0; } } printf("NO"); return 0; }

C Program to check Perfect Square using sqrt() function:

We can perform the same program getting help from sqrt() function. The sqrt() function is bundled with math.h header file. Here is the program:

/* C Program to check Perfect Square using sqrt function */ #include <stdio.h> #include <math.h> int main( { int n, temp; printf("Enter a number: "); scanf("%d",&n); temp = sqrt(n); if(temp*temp == n) printf("YES."); else printf("NO."); return 0; }

Share:

In this example, we will see a C program through which we can check if a given number is a perfect square or not.

If a whole number is the square of another whole number then it is known as a perfect square, like 16 is the square of 4 so 16 will be called a Perfect square.

Algorithm:
  • STEP 1:Input any number x.
  • STEP 2:Store its square root in a float variable fVar.
  • STEP 3:Assign fVar into iVar (an integer variable) iVar=fVar.
  • STEP 4:Now compare iVar and fVar value will be equal. If the number does not a perfect square, iVar and fVar will not same.
/*C program to check number is perfect square or not.*/ #include <stdio.h> #include <math.h> int main() { int num; int iVar; float fVar; printf("Enter an integer number: "); scanf("%d",&num); fVar=sqrt((double)num); iVar=fVar; if(iVar==fVar) printf("%d is a perfect square.",num); else printf("%d is not a perfect square.",num); return 0; }

Output: Enter an integer number: 64

64 is a perfect square.

Given a number, check if it is a perfect square or not. 

Input : 2500 Output : Yes Explanation: 2500 is a perfect square. 50 * 50 = 2500 Input : 2555 Output : No

#include <bits/stdc++.h>

using namespace std;

bool isPerfectSquare(long double x)

{

    if (x >= 0) {

        long long sr = sqrt(x);

        return (sr * sr == x);

    }

    return false;

}

int main()

{

    long long x = 2502;

    if (isPerfectSquare(x))

        cout << "Yes";

    else

        cout << "No";

    return 0;

}

class GFG {

    static boolean isPerfectSquare(int x)

    {

        if (x >= 0) {

            int sr = (int)Math.sqrt(x);

            return ((sr * sr) == x);

        }

        return false;

    }

    public static void main(String[] args)

    {

        int x = 2502;

        if (isPerfectSquare(x))

            System.out.print("Yes");

        else

            System.out.print("No");

    }

}

import math

def isPerfectSquare(x):

    if(x >= 0):

        sr = int(math.sqrt(x))

        return ((sr*sr) == x)

    return false

x = 2502

if (isPerfectSquare(x)):

    print("Yes")

else:

    print("No")

using System;

class GFG {

    static bool isPerfectSquare(double x)

    {

        if (x >= 0) {

            double sr = Math.Sqrt(x);

            return (sr * sr == x);

        }

        return false;

    }

    public static void Main()

    {

        double x = 2502;

        if (isPerfectSquare(x))

            Console.WriteLine("Yes");

        else

            Console.WriteLine("No");

    }

}

<?php

function isPerfectSquare($x)

{

    $sr = sqrt($x);

    return (($sr - floor($sr)) == 0);

}

$x = 2502;

if (isPerfectSquare($x))

    echo("Yes");

else

    echo("No");

?>

<script>

function isPerfectSquare(x)

    {

        if (x >= 0) {

            let sr = Math.sqrt(x);

            return ((sr * sr) == x);

        }

        return false;

    }

        let x = 2500;

        if (isPerfectSquare(x))

            document.write("Yes");

        else

            document.write("No");

</script>

Time Complexity: O(log(x))
Auxiliary Space: O(1)

To know more about the inbuilt sqrt function, refer this Stackoverflow and this StackExchange threads.

#include <iostream>

#include <math.h>

using namespace std;

void checkperfectsquare(int n)

{

    if (ceil((double)sqrt(n)) == floor((double)sqrt(n))) {

        cout << "perfect square";

    }

    else {

        cout << "not a perfect square";

    }

}

int main()

{

    int n = 49;

    checkperfectsquare(n);

    return 0;

}

import java.io.*;

class GFG{

static void checkperfectsquare(int n)

{

    if (Math.ceil((double)Math.sqrt(n)) ==

        Math.floor((double)Math.sqrt(n)))

    {

        System.out.print("perfect square");

    }

    else

    {

        System.out.print("not a perfect square");

    }

}

public static void main(String[] args)

{

    int n = 49;

    checkperfectsquare(n);

}

}

import math

def checkperfectsquare(x):

    if (math.ceil(math.sqrt(n)) ==

       math.floor(math.sqrt(n))):

        print("perfect square")

    else:

        print("not a perfect square")

n = 49

checkperfectsquare(n)

using System;

class GFG{

static void checkperfectsquare(int n)

{

    if (Math.Ceiling((double)Math.Sqrt(n)) ==

        Math.Floor((double)Math.Sqrt(n)))

    {

        Console.Write("perfect square");

    }

    else

    {

        Console.Write("not a perfect square");

    }

}

public static void Main()

{

    int n = 49;

    checkperfectsquare(n);

}

}

<script>

function checkperfectsquare(n)

{

    if (Math.ceil(Math.sqrt(n)) ==

        Math.floor(Math.sqrt(n)))

    {

        document.write("perfect square");

    }

    else

    {

        document.write("not a perfect square");

    }

}

let n = 49;

checkperfectsquare(n);

</script>

Time Complexity : O(sqrt(n))