
November 18th, 2012, 05:46 PM
|
|
Contributing User
|
|
Join Date: Oct 2011
Posts: 43
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
|
|
|
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;
}
|