+ 1
What?
What is the output of this code? int x; string y="''; for (x = x x<= 30; x++) { if (x % 2 == x % 4) break; y += Convert. ToString(x); Console. Write (y); Why is y 23??? At the time when x=4 in the loop the bool is true or not? I mean 4%2=0 and 4#4 is also 0, which means 2%2=0=4%2. in this case y should be 4 ?
15 Antworten
+ 5
Pay attention to the details:
https://sololearn.com/compiler-playground/cV7tnsyz1O7O/?ref=app
Note that x initially has the value 2.
2 % 2 == 2 % 4 --> false
3 % 2 == 3 % 4 --> false
4 % 2 == 4 % 4 --> true
Hence y = "23"
+ 3
the closing } is missing.
x is not initialized and the head of the for-loop is missing a a semicolon. There are blamknspaces between Convert. and ToString as well as between Console. and Write.
+ 3
Nico Philipp, make a post on your channel, attach a screenshot and I will see it.
+ 2
I believe you are missing the fact that you are converting numbers to a string and the confusion on the
if condition:
if 4%2 equals 0 and 4%4 equals 0 break;
Full code:
int x;
string y="";
for (x=2; x<=30; x++){
Console.WriteLine(x);
if (x%2 == x%4)
break;
y += Convert.ToString(x); // the first number is 2 then 3 which is string plus string not a number
Console.WriteLine(y);
}
// This is a string not a number "2" concat "3" or "23"
Console.Write(y);
+ 1
Tag the relevant programming language.
Show the complete code.
Check quotation marks, semicolons, blank spaces, and matching brackets.
+ 1
The code will produce a compilation error. No output.
0
The language is c#. I dont have mor code, this was a question in the quiz duel. I just dont get it 🤔
0
Nico Philipp, your code is is syntactically incorrect. Lisa is right. But I don't understand why you need this code? In any range, up to 30, up to 100, or up to 1,000,000, the y number will be equal to 0, since 0 corresponds to the condition in the if. I corrected all the errors and typos in your code.
https://sololearn.com/compiler-playground/cQJSJ4GH2tm8/?ref=app
0
Guys its not my code its a code from the quiz duel… i just hopef someone could help me understand why the answer was 23 and not 4
0
Nico Philipp, I probably didn't quite understand the meaning and sequence of the code presented in your question because it had a lot of errors. So I had to redo it. I got 0.
0
If i could i would post the screenshot of the question in the quiz maybe it could help but i dont know how
0
Alright i did it
0
Oh okay … i think i understand it now thanks 😄
0
The reason why y = 23 is because the break stops the loop before adding the value of x when the condition x % 2 == x % 4 becomes true.
For example, when x = 4:
4 % 2 = 0 and 4 % 4 = 0, so the condition is true, and the loop breaks.
But 4 is not added to y because the break happens before the line y += Convert.ToString(x).
The numbers added before the break were 2 and 3, which is why y ends up being "23".
In other words, the loop stops before 4 can be appended to the string.