While loop error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

While loop error

I don't understand why program doesn't work. It should print 3,6,9,12,15,18; #include <iostream> using namespace std; int main() { //change the code int num = 0; while(num<=20){ if (num%3=0) cout <<num; else num+=1; return 0; }

31st Jul 2021, 1:13 PM
vilrau12
vilrau12 - avatar
6 Answers
+ 5
There are a few things, you can see in the update as follows: #include <iostream> using namespace std; int main() { //change the code int num = 0; while(num<=20){ if (num%3==0) cout <<num<<" "; // else num+=1; } return 0; }
31st Jul 2021, 1:18 PM
JaScript
JaScript - avatar
+ 3
Not all can see the pro tasks.
31st Jul 2021, 1:26 PM
JaScript
JaScript - avatar
+ 2
Thanks I see what I did wrong, but the program still doesn't work properly..
31st Jul 2021, 1:23 PM
vilrau12
vilrau12 - avatar
+ 1
So i have this: #include <iostream> using namespace std; int main() { //change the code int num = 0; while(num<=20){ cout<<num<<endl; num+=1; } return 0; } and I need to change the program to only print 3,6,9,12,15,18 instead of all from 1 to 20
31st Jul 2021, 1:29 PM
vilrau12
vilrau12 - avatar
+ 1
#include <iostream> #include <string> using namespace std; int num=20; while (num >= 1){ num-=1; If((num%3)==0){ cout<<num; continue; } else{ continue; } } /*IMHO this should work*/
31st Jul 2021, 10:30 PM
Sanjay Kamath
Sanjay Kamath - avatar
0
yes thank you, i didn't know this ++num thing thanks
31st Jul 2021, 1:48 PM
vilrau12
vilrau12 - avatar