This answer is what?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

This answer is what??

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int x = 1; while(x++ < 5) { if (x%2==0) { x+=2; } } Console.WriteLine(x); } } } output 6 but answer question this level is number 2 what???

8th Jan 2017, 8:29 PM
No Name
7 Answers
+ 6
so, x start as 1. is x%2 ==0? nope, so next loop. x its 2 now. its x%2 == 0? yes, so x+=2 . x its 4 now. Its 4%2==0? yes, so x+=2. x is 6, end of while, print x.
8th Jan 2017, 8:35 PM
Nahuel
Nahuel - avatar
+ 2
read the question again. The output of x is not the solution ;)
23rd Jan 2017, 11:35 PM
H3llsing
H3llsing - avatar
+ 2
I don't think I fully understand how 6 is the answer. Nor will I be the last person to read and question this. So for clarification purposes(?). I get how 'x+=2' is simply; take 'x' and add '2', then reassigning 'x' as the new value. The '%' means Module, but what is 'x' being divided by? Is the question referring to x / 2 with a remainder of exactly equal to 0? - Thankx in advance.
4th Feb 2017, 10:19 AM
William Draper
William Draper - avatar
+ 1
this answer in this emulator and visual studio and trace in paper is 6 why answer in question in app is 2?
10th Jan 2017, 3:30 PM
No Name
10th Jan 2017, 3:55 PM
No Name
+ 1
only 2 and 4 would have no remainder if divided by 2 satisfying the %2==0 statement. therefore the answer is two outputs.
19th Jan 2017, 6:37 PM
Steven Bennett
Steven Bennett - avatar
+ 1
Bruce-Michael Draper Yes, it evaluate the remainder of division. Generally, the module '%' returns remainder of division. For ex.: You know that 5/2=2 ('/' returns integer value and drop the remainder), so: 5%2=5-((5/2)*2) Generally: x%y=x-((x/y)*y) where y != 0
16th Feb 2017, 11:15 PM
Asomnia
Asomnia - avatar