Question:
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }
Answer:
Ans is i hate u because the of float and doubles are different. if condition is false. Source: CoolInterview.com
When float is assigned,by default it will take 1.1 as double. When it is converted to float, it truncates the value (say 1.09999.. not exactly). So in the if statement there will be a difference,hence else part gets execute. Even if we try to print the values of "me" , "you" it will give the same result.Instead of that it will execute else part.
main() { float me = 1.1f; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }
Now the output will be the if part that is "I Love You"
Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date:
| Contact Syed Baseer Ahmed
Hey, i checked it on different compilers its giving I hate U only. So the above explanation is a bit wrong. Sorry for that.
Actually, main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }
Here if we debug ( not to print ) we get values as me = 1.10000002 you = 1.1000000000000001
Hence those two values will never be equal. So it will always give I hate U
I am really sorry for the previous explanation. Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date:
| Contact Syed Baseer Ahmed
The output will be "i Hate You". In the above the comparison is between two different data types (float and double) . The operator(comparison) is applicable for the variables of same data type. Source: CoolInterview.com
Answered by: Arjun | Date: 1/22/2008
| Contact Arjun
The answer is "i hate u", because even though the values of me and you are same,but the data types are different.After the decimal point,the precision is up to 8 values for float and for double is up to 16 values.so the answer is "i hate you ". Source: CoolInterview.com
Answered by: lavanya | Date: 2/29/2008
| Contact lavanya
These both float and double are used to print the value in decimal.But I think that by default float takes 6 value after decimal and double takes 12 value after decimal.For example: float me=1.1 when we print me ,its will give 1.100000.Similarly double you=1.100000000000. Source: CoolInterview.com
Answered by: sonu | Date: 12/1/2008
| Contact sonu
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|