0
C++ 1 TO 10 AND REVERSE LOOP
my output should be: 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0 this is my code the number 9 is disappearing pls helpp NOTE: dont change int y= 9 because i already tried it since there should be a 0 at the end https://www.sololearn.com/compiler-playground/c3sLQ7r55Q50
7 Réponses
+ 6
soupie Maybe you could initialize "y" with 9 instead of 8?
(And it looks like you initialized x twice, therefore the warning message.)
Jayakrishna🇮🇳 , Snehil Pandey Thank you!!!
+ 4
Jayakrishna🇮🇳 How do we need to change the links to make it accessible using the app?
+ 4
Lisa
in the link, Replace 'www.' with 'code.'
Remove complier-playground/
https://code.sololearn.com/c3sLQ7r55Q50
edit:
soupie
you can try int y = 9; then condition x<=20 instead of x<20
+ 3
https://code.sololearn.com/cIm4q1sAAxy4/?ref=app
Lisa check this but it's in python
[This is not the answer]
+ 3
soupie it works if you initialize y = 9, and adjust the end condition of the loop to x<=20 (insert =).
There is also a mathematical solution. The "V" shape of the output is characteristic of the absolute value function. You have to #include the math library to get abs(). This gives the desired output:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
for(int x = -9; x <= 10; x++)
cout << 10-abs(x) << "\n";
return 0;
}
+ 2
here is my easy and simple approach
https://code.sololearn.com/c3csPk5w8uWo/?ref=app