0

Please help me to find out what is wrong

My question is : You are given a program that outputs all the numbers from 0 to 20. Change the code to make it output only numbers that are multiples of 3. Note :Be attentive and don't output 0. My conte is : #include <iostream> using namespace std; int main() { int num = 0; while(num<=20){ num+=3; if (num %3==0) cout << num << endl; } return 0; } My output is : 3 6 9 12 15 18 21 But expected output : 3 6 9 12 15 18 How can I remove 21 from my code? Edit : I found the answer. https://code.sololearn.com/cmjqMRTEfp1H/?ref=app

27th Jan 2021, 2:54 PM
Mustaq Mahmud Tafhim
Mustaq Mahmud Tafhim - avatar
6 Answers
+ 10
just move the update statement (num += 3) after printing the result. https://code.sololearn.com/cr08RlY26jsQ/?ref=app
27th Jan 2021, 2:58 PM
Arsenic
Arsenic - avatar
+ 5
You could also just change the condition to while(num < 18) and keep the rest the same.
27th Jan 2021, 3:03 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You have to change int num = 0 to int num = 3 😊😊😊
3rd Apr 2021, 5:56 PM
Y’ello Code
Y’ello Code - avatar
0
If you want to respect the text of the problem... "You are given a program that outputs all the numbers from 0 to 20. Change the code to make it output only numbers that are multiples of 3. Be attentive and don't output 0." ... this is the answer: #include <iostream> using namespace std; int main() { //change the code int num = 0; while(num<=20){ if ((num != 0) && (num %3 == 0)) cout<<num<<endl; num+=1; } return 0; }
29th Jul 2021, 9:46 AM
Octavian Malinici-Pruteanu
Octavian Malinici-Pruteanu - avatar
0
/*im using this, is it the right way? i change this : while (num +1<=20) */ #include <iostream> using namespace std; int main() { //change the code int num = 3; while(num+1 <=20){ cout<<num<<endl; num+=3; } return 0; }
15th Jun 2022, 1:40 PM
Matthew Mamoto
Matthew Mamoto - avatar
0
All of your answers do not meet the requirements of the problem: "outputs all the numbers from 0 to 20. Change the code..." 😉 Don't use "num+=3"... start from num=0... check if ((num != 0) && (num %3 == 0))
15th Jun 2022, 2:58 PM
Octavian Malinici-Pruteanu
Octavian Malinici-Pruteanu - avatar