0

More methods of solving The while Loop assignment.

I have solved the assignment in the following way : - #include <iostream> using namespace std; int main() { //change the code int num = 3; while(num<=20){ cout<<num<<endl; num += 3; } return 0; However, I felt it was too simple to be true. Are there any more ways of solving the same problem?

21st May 2021, 2:22 AM
Xiveski
Xiveski - avatar
2 Réponses
+ 1
I suppose another method of solving it could be using a conditional statement with a modulous and checking it against every increment, like so: int num = 3; while(num <=20) { if(num % 3 == 0) { cout<<num<<endl; } num++; }
21st May 2021, 3:49 AM
Kylie
Kylie - avatar
+ 1
Using for loop instead of while : for(int num = 3 ; num<=20; num+=3) cout<<num<<endl;
21st May 2021, 3:52 AM
‎Abdul Jalil
‎Abdul Jalil - avatar