0
How do I output only even numbers in C#
Iām stuck on C# 15.2 practice Iāve done this code below which outputs all numbers going from 1 to the number the console input. It says int numbers that can be divided by 2 are even numbers but I donāt know what todo with this information. int num = Convert.ToInt32(Console.ReadLine()); int res = 1; //your code goes here while(++res<=num) Console.WriteLine(res);
3 Answers
+ 2
If you only want to output even numbers, you can start res at 2 and then just increment res by 2 for each iteration up to num.
If you want to check if a number is even (or evenly divisible by 2) you can use the modulo operator and check that the remainder is 0.
num % 2 == 0 // true if even false otherwise.
+ 1
I finally did it after 2+ hours š lol
https://sololearn.com/coach/665/?ref=app
+ 1
https://code.sololearn.com/c3wbruv24gZM/?ref=app