+ 2

C# The While Loop 15.2 Practice Calculating Range

So I used the following code to get through the practice question, but is there a more logical way of writing this? I feel like I kinda cheated because I changed the initialization of res to 2, from 1. Thanks for the feedback. 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 num = Convert.ToInt32(Console.ReadLine()); int res = 2; while(res <= num) { Console.WriteLine(res); res+=2; } } } }

14th Jul 2021, 4:09 PM
Keven Beasley
Keven Beasley - avatar
2 ответов
+ 8
//try this int num = Convert.ToInt32(Console.ReadLine()); int res = 1; while (num>=res) { if(res%2==0){ Console.WriteLine(res); } res++; }
14th Jul 2021, 4:54 PM
Simba
Simba - avatar
0
Hi, can you explain me in more details please ? I don't understand at all what's going on !
28th Aug 2021, 1:51 AM
Akira Akira
Akira Akira - avatar