Here's a C program to check whether a given number is a perfect square or not with output and proper explanation. Here, we're using math.h header file to use the square root function i.e. sqrt().

What is a Perfect Square?A number is a perfect square if the square root of that number is an integer value e.g. square root 81 is 9 i.e. an integer while square root of 8 is not an integer.

# include <stdio.h> # include <conio.h> # include <math.h> void main() { int m, n ; float p ; clrscr() ; printf("Enter a number : ") ; scanf("%d", &n) ; p = sqrt(n) ; m = p ; if (p == m) printf("\n%d is a perfect square", n) ; else printf("\n%d is not a perfect square", n) ; getch() ; }

**Output of above program is**

Enter a number : 36

36 is a perfect square

Enter a number : 15

15 is not a perfect square

**Explanation of above program**

Here we're using another header file

**math.h**. This header file contains several maths operation like sqrt() to calculate square root etc. which can only be available after including math.h header file in your program.
Then the program asks the user to enter a number and stores it in an integer variable

**n**. After that we're calculating the square root of the number n and storing it in a variable**p**with**float data type**.Note:When you take square root of a number the result is always of type float .e.g sqrt(81) = 9.00

After calculating the square root of our number, we're assigning this float value to another integer variable

**m**so that m will contain the integer part of float variable p. After that using an If-else condition we're checking if the float variable p and integer variable m are equal and if they're that means the number is a perfect square.

Suppose n = 36. The steps are as follows -

- First we calculate the square root of p by - p = sqrt(n) = sqrt(36) = 6.00.
- Then we assign value of p to m (integer type) - m = p or m = 6.00 or m = 6.
- After that we compare values of m and p in an IF condition i.e.
**p == m**or**6.00 == 6**. Now even when the data types of these two variables don't match but still their values are same so the IF condition will evaluate to be true making the number n a perfect square. - Now suppose n = 2. Therefore, p = 1.414 and m = 1. So when we compare p and m (
**i.e. 1.414 == 1**) the value will be false and the else condition will be executed in this case.

#include

#include

int main(void)

{

int number;

int k;

double result;

number = 6400;

result= sqrt (number);

k = sqrt (number);

if (result - k == 0)

printf ("\n The integer HAS a perfect square \n\n");

else

printf ("\n The integer DOES NOT HAVE a perfect square \n\n");

return 0;

}

That is another way of doing it but why call SQRT() twice when you can achieve the same thing with just function call? Also, instead of double, float will cost you less memory.

Anyways, thanks for sharing your approach with us... :)

return Math.sqrt(n) % 1 === 0;

php switch

identifier in python learn programming in free