Our social:

C Program to check Perfect Square

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


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;
}