+ 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???
7 Respostas
+ 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.
+ 2
read the question again. The output of x is not the solution ;)
+ 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.
+ 1
this answer in this emulator and visual studio and trace in paper is 6
why answer in question in app is 2?
+ 1
http://www.sololearn.com/app/csharp/playground/cnvBTubH7B7u/
this is code this app
+ 1
only 2 and 4 would have no remainder if divided by 2 satisfying the %2==0 statement. therefore the answer is two outputs.
+ 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