+ 25
Why is 1 in the code the wrong answer and what is the right one and why ?
" The for loop" exercise is: Fill in the blanks to print even values from 0 to 20. for (int x=0; x<=20 x+=1){ cout <<x<< endl; }
17 Réponses
+ 17
thank you 😊
+ 16
the answer lies in "To print even values" so you have to add x+=2 to print:
2
4
6
8
10
...
up to 20
+ 14
okay, yes I understand it
I thought about 1, 2, 3 ... till 20 :)
+ 10
If x+=1 is used, x is incremented by 1 in every iteration. So, every number from 0 to 20 is displayed.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
If x+=2 is used, x is incremented by 2 in every iteration. So, every even number from 0 to 20 is displayed.
0 2 4 6 8 10 12 14 16 18 20
And, semicolon is missing before incrementation.
+ 5
your question has been answered but I thought you might also want to know that although 'x+=1' works correctly, you might want to use 'x++' in place of that because its more generally accepted and easier to type
+ 5
need to add 2 to print even value.. x += 2 ;
+ 4
If this was python you could use an if statement to test if modulus 2 ==0 and print it out or cout in this case. But in the case of c++ , using modulus with if statement will just add another statement.... Think of it as waste of resources.
+ 2
simple is that we want even numbers that why we increment i value by 2.
+ 1
4-2=2; 6-4=2; 8-6=2...So it's 2.
+ 1
your for its wrong missing semicolons
for (int x=0; x<=20; x+=1) { ...
}
0
hey jas
remember that the compiler could pass some programmer's mistakes
for (int x=0; x<=20 x+=1){
needs a ";" after 20 and must be like this
for (int x=0; x<=20; x+=2){
that is one reason many people hates "C" language 😢
😆😆😆
0
c language is more complex than other languages so carefully go through it, and put x+=2 to get your result. if(x=0;x<=20;x+=2)
0
it says to give even numbers and ur starting value is 0 so u have to aplly +2 to get even numbers
0
the increment should be done by 2 not 1
- 2
I want to help u..but I can't help..because I don't know a lot of c++ ..I am really sorry
- 3
+Jasmin , did you learned web languges 1st or c++ and oop 1st ?,