0

How to fix the bug?

int x; int y; cin>>x; cin>>y; if( x < 1000){ while ( x < 999) { x = x + y; cout << x << endl; } } When I give x and y's values like x=50,y=20 it prints 70 to 1010 . But it must have printed 70 to 980 . What's wrong ??

20th Sep 2020, 11:45 AM
Ali_combination
Ali_combination - avatar
8 Antworten
+ 3
Check this if its what you want:: https://code.sololearn.com/c0zxE3PKlVQP/?ref=app
20th Sep 2020, 12:23 PM
Rawley
Rawley - avatar
+ 2
When value reaches 990 it checks if x<999 since it is loop runs again and adds 20 more ,which results in 1010 and you exit out of the while and if loop after that ,Perhaps you may want to check your calculations again
20th Sep 2020, 11:52 AM
Abhay
Abhay - avatar
20th Sep 2020, 11:56 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
Ok good, can i delete the code now?
20th Sep 2020, 2:12 PM
Rawley
Rawley - avatar
0
Yeah i was mistaken.. it prints from 70 to 990 . But it shouldn't print 1010 cause 990 is less than 1000.
20th Sep 2020, 11:56 AM
Ali_combination
Ali_combination - avatar
0
Now what should i do to not include 1010 when x=990? . Plus its uncanny to me because when i write the code that i learned from "while loop" part , it doesn't print 1010 while there's no difference between what Sololearn has write and my own program😅
20th Sep 2020, 12:04 PM
Ali_combination
Ali_combination - avatar
0
That's exactly what I need🤔
20th Sep 2020, 2:11 PM
Ali_combination
Ali_combination - avatar
0
Feel free bro♥️
20th Sep 2020, 2:13 PM
Ali_combination
Ali_combination - avatar