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?
2 odpowiedzi
+ 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++;
}
+ 1
Using for loop instead of while :
for(int num = 3 ; num<=20; num+=3)
cout<<num<<endl;