1 1 vote int main() { int a, b; /* Some code which initializes "a" here ... */ if (a < 0) { a = -a; } b = sqrt (a); } Above code will always work for calculating the square root of any valid integer value on a system . [T/F] Programming in C programming-in-c + – dd 2.6k views answer comment Share Follow Print See all 6 Comments 6 6 Comments reply air1 commented Dec 27, 2016 reply Follow flag how will it give correct answer for any non perfect square? 0 0 replyShare thor commented Dec 27, 2016 reply Follow flag by default sqrt() is of type double. 0 0 replyShare dd commented Dec 27, 2016 reply Follow flag Neglect b truncation. Other than that 0 0 replyShare dd commented Dec 27, 2016 reply Follow flag b I'll be integer. That's obvious. You can choose appreciate type for b, and make sqrt result as precise as possible. Other than this problem ? 0 0 replyShare thor commented Dec 27, 2016 reply Follow flag no. No problem for any valid integer on system. 0 0 replyShare pC commented Dec 27, 2016 reply Follow flag @Debashish_deka but the validity of this statement will depend on compiler right ? Will we able to get a general answer for this ? I really doubt this get executed correctly always. Moreover for large -ve numbers , will it work ? 0 0 replyShare Please log in or register to add a comment.
2 2 votes #include <stdio.h> int main() { int a=-4, b; /* Some code which initializes "a" here ... */ if (a < 0) { a = -a; } b = sqrt (a); printf("%d",b); return 0; } yes code will always work. But will not give correct output everytime. Like for - 4 it is giving square root as 2. But it is not a correct result srestha answered Dec 27, 2016 srestha comment Share Follow See all 16 Comments 16 16 Comments reply Show 13 previous comments Arjun commented Dec 27, 2016 reply Follow flag ^What has 32768 to do with int? 0 0 replyShare srestha commented Dec 27, 2016 reply Follow flag Sir, I mean int range in between -32768 to 32767 But but output here showing 32768. that is why it fails in INT_MIN rt? 0 0 replyShare Arjun commented Dec 27, 2016 reply Follow flag int range in C is never that - that was in stone age. C standard does not fix the size of int, but most compilers take it as 4 bytes. So, range is $-2^{31} - (2^{31} - 1)$ if system uses 2's complement representation (this is also not mandated by C) and since there is one extra number possible for negatives, it fails for INT_MIN. 2 2 replyShare Please log in or register to add a comment.