November 18th, 2012, 05:46 PM
-
Perfect Square function not working properly?
It should return false when the number does not have perfect square, otherwise it returns the root. But in this code it returns the root all the time. E.g. Input 5, root 2.
Code:
main()
{
int i;
int number=0;
int result=0;
for(i=0; i<10; i++){
printf("Testing:");
scanf("%i",&number);
result = isSquare(number);
if(result==0)
printf("Fail\n");
else
printf("%i\n",result);
}
}
int isSquare(int n)
{
float root = sqrt(n);
if (n == (int) n)
return root;
else
return 0;
}
November 18th, 2012, 06:46 PM
-
Note what you're testing here.
Code:
float root = sqrt(n);
if (n == (int) n)
return root;