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);

17th Mar 2021, 11:45 PM
Matt Gisby
Matt Gisby - avatar
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.
18th Mar 2021, 12:17 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
I finally did it after 2+ hours šŸ˜­ lol https://sololearn.com/coach/665/?ref=app
18th Mar 2021, 12:40 AM
Matt Gisby
Matt Gisby - avatar
+ 1
https://code.sololearn.com/c3wbruv24gZM/?ref=app
19th Mar 2021, 6:13 PM
hossein B
hossein B - avatar