Check If a Number Is a Perfect Square or Not in C / C++ / Java / Python / C#

Perfect square

A number is called a perfect square if the square root of this number is an integer. For example, 16 is a perfect square as the square root of this number are 4 and -4, which are integers. Programs to check if a number is a perfect square or not are shown here.






1. Algorithm to Check If a Number Is a Perfect Square or Not


1. Take a whole number x as input.

2. Check if both the ceiling and floor values of the square root of x are equal.

3. If step 2 is true declare x as a perfect square.

4. If step 2 is false declare x as not a perfect square.




2. Pseudocode to Check If a Number Is a Perfect Square or Not


Input : A whole number $x$

Output : $x$ is a perfect square or not

1. Procedure perfectSquare($x$):

2. If $\lceil{\sqrt{x}}\rceil == \lfloor{\sqrt{x}}\rfloor$:

3. Return "Perfect Square"

4. Else:

5. Return "Not a Perfect Square"

6. End Procedure





3. Time complexity to Check If a Number Is a Perfect Square or Not


Time Complexity: O($\sqrt{n}$)

Where $n$ is the input number.





4. Flowchart to Check If a Number Is a Perfect Square or Not

Flowchart to Check If a Number  Is a Perfect Square or Not



5. Program to Check If a Number Is a Perfect Square or Not

This section covers the C, C++, Java, Python and C# programs to check if a number is a perfect square or not. The programs compare the ceiling and floor values of the square root of the input number. If both values are the same, the number is considered a perfect square.




5.1. C Program to Check If a Number Is a Perfect Square or Not

Code has been copied
/*******************************
      alphabetacoder.com
C program to check if a number 
is a perfect square or not
********************************/

#include <stdio.h>
#include <math.h>

int main() {
    // declare variables
    int n;

    // take input 
    printf("Enter a number: ");
    scanf("%d", & n);

    // check if ceiling and floor value
    // of square root of n are same or not
    // If the values are same, then n is a perfect square
    if (ceil((double) sqrt(n)) == floor((double) sqrt(n)))
        printf("%d is a perfect square", n);
    else
        printf("%d is not a perfect square", n);

    return 0;
}

Output


Case 1:

Enter a number: 36

36 is a perfect square


Case 2:

Enter a number: 100

100 is a perfect square


Case 3:

Enter a number: 20

20 is not a perfect square




5.2. C++ Program to Check If a Number Is a Perfect Square or Not

Code has been copied
/*******************************
      alphabetacoder.com
C++ program to check if a number 
is a perfect square or not
********************************/

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    // declare variables
    int n;

    // take input 
    cout << "Enter a number: ";
    cin >> n;

    // check if ceiling and floor value
    // of square root of n are same or not
    // If the values are same, then n is a perfect square
    if (ceil((double) sqrt(n)) == floor((double) sqrt(n)))
        cout << n << " is a perfect square";
    else
        cout << n << " is not a perfect square";

    return 0;
}

Output


Case 1:

Enter a number: 49

49 is a perfect square


Case 2:

Enter a number: 125

125 is a perfect square


Case 3:

Enter a number: 43

43 is not a perfect square




5.3. Java Program to Check If a Number Is a Perfect Square or Not

Code has been copied
/*********************************
      alphabetacoder.com
Java program to check if a number 
is a perfect square or not
**********************************/

import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        // declare variables
        int n;

        // declare an instance of scanner class
        Scanner sc = new Scanner(System.in);

        // take input 
        System.out.print("Enter a number: ");
        n = sc.nextInt();

        // check if ceiling and floor value
        // of square root of n are same or not
        // If the values are same, then n is a perfect square
        if (Math.ceil((double) Math.sqrt(n)) == Math.floor((double) Math.sqrt(n)))
            System.out.print(n + " is a perfect square");
        else
            System.out.print(n + " is not a perfect square");
    }
}

Output


Case 1:

Enter a number: 81

81 is a perfect square


Case 2:

Enter a number: 144

144 is a perfect square


Case 3:

Enter a number: 5

5 is not a perfect square




5.4. Python Program to Check If a Number Is a Perfect Square or Not

Code has been copied
# ***********************************
#      alphabetacoder.com
# Python program to check if a number
# is a perfect square or not
# ***********************************

import math

# take input
n = int(input("Enter a number: "))

# check if ceiling and floor value
# of square root of n are same or not
# If the values are same, then n is a perfect square
if math.ceil(math.sqrt(n)) == math.floor(math.sqrt(n)):
    print(n, "is a perfect square")
else:
    print(n, " is not a perfect square")

Output


Case 1:

Enter a number: 100

100 is a perfect square


Case 2:

Enter a number: 64

64 is a perfect square


Case 3:

Enter a number: 50

50 is not a perfect square




5.5. C# Program to Check If a Number Is a Perfect Square or Not

Code has been copied
/*******************************
      alphabetacoder.com
C# program to check if a number 
is a perfect square or not
********************************/

using System;

namespace PerfectSquare {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int n;

            // take input 
            Console.Write("Enter a number: ");
            n = Convert.ToInt32(Console.ReadLine());

            // check if ceiling and floor value
            // of square root of n are same or not
            // If the values are same, then n is a perfect square
            if (Math.Ceiling((double) Math.Sqrt(n)) == Math.Floor((double) Math.Sqrt(n)))
                Console.WriteLine(n + " is a perfect square");
            else
                Console.WriteLine(n + " is not a perfect square");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter a number: 121

121 is a perfect square


Case 2:

Enter a number: 16

16 is a perfect square


Case 3:

Enter a number: 33

33 is not a perfect square