Home >>c programs >C program to check whether number is Perfect Square or not

C program to check whether number is Perfect Square or not

C program to check whether number is Perfect Square or not

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.

No Sidebar ads