Why does my program hv no output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does my program hv no output

#include <stdio.h> #include <stdlib.h> int main() { float x = 1.1; while(x == 1.1) { printf("x = %f",x); x = x - 0.1; } return 0; } https://code.sololearn.com/ckongjTmvl2r/?ref=app

3rd May 2022, 4:53 AM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
14 Answers
+ 4
Zane Vijay Falcao If you use double instead of float then condition will be true double x = 1.1; if (x == 1.1) //true float y = 1.1; if (y == 1.1) //false
3rd May 2022, 9:04 AM
A͢J
A͢J - avatar
+ 3
Zane Vijay Falcao Both x = 1.1 and 1.1 seems equal but that's not So do this: while (x > 1.1) Or do this: while(x == (float) 1.1)
3rd May 2022, 8:51 AM
A͢J
A͢J - avatar
+ 3
Thanks
3rd May 2022, 9:05 AM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
+ 1
It seems you have an infinite loop, then execution gets aborted. Pls link your code from Code Playground into the question (edit the question) for us to see the exact response.
3rd May 2022, 11:14 AM
Emerson Prado
Emerson Prado - avatar
3rd May 2022, 11:17 AM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
+ 1
It is really an issue of precision. Pls read here a good reference on comparing float values: https://www.tutorialspoint.com/what-is-the-most-effective-way-for-float-and-double-comparison-in-c-cplusplus
3rd May 2022, 11:27 AM
Emerson Prado
Emerson Prado - avatar
0
The question is to predict the output n explain Y don't get a output
3rd May 2022, 6:47 AM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
0
The value of test condition is true so it should execute the printf statement
3rd May 2022, 6:50 AM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
0
(float) means type casting right??
3rd May 2022, 8:58 AM
Zane Vijay Falcao
Zane Vijay Falcao - avatar
0
Erm no idea
4th May 2022, 6:17 PM
Arda Das
0
try this: printf("%d\n", x==1.1) x==1.1 is 0 while(0) will never get executed.
5th May 2022, 4:17 AM
Bob_Li
Bob_Li - avatar
- 1
trt while(x<=1.1)
3rd May 2022, 6:45 AM
Abhinav k
- 1
it may be because you used == symbol inside the while
3rd May 2022, 6:48 AM
Abhinav k