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

27th Oct 2022, 2:28 PM
soupie
7 Respostas
+ 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!!!
27th Oct 2022, 2:40 PM
Lisa
Lisa - avatar
+ 4
Jayakrishna🇮🇳 How do we need to change the links to make it accessible using the app?
27th Oct 2022, 2:29 PM
Lisa
Lisa - avatar
+ 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
27th Oct 2022, 2:34 PM
Jayakrishna 🇮🇳
+ 3
https://code.sololearn.com/cIm4q1sAAxy4/?ref=app Lisa check this but it's in python [This is not the answer]
27th Oct 2022, 2:30 PM
Just Some Random Guy
Just Some Random Guy - avatar
+ 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; }
29th Oct 2022, 1:22 AM
Brian
Brian - avatar
+ 2
here is my easy and simple approach https://code.sololearn.com/c3csPk5w8uWo/?ref=app
28th Oct 2022, 7:36 PM
Sunil Shrestha
Sunil Shrestha - avatar
+ 2
Brian There are many approaches to solve the question, you can also have: #include <iostream> using namespace std; int main() { for(int i=1,n=1;i>0;i+=n){ if(i==10) n=-1; cout<<i<<endl; } return 0; } But soupie has asked for corrections in his code not for new approach
29th Oct 2022, 5:20 AM
Just Some Random Guy
Just Some Random Guy - avatar