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
6 Answers
+ 10
just move the update statement (num += 3) after printing the result.
https://code.sololearn.com/cr08RlY26jsQ/?ref=app
+ 5
You could also just change the condition to
while(num < 18)
and keep the rest the same.
+ 1
You have to change int num = 0 to int num = 3 đđđ
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;
}
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;
}
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))