+ 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; } } } }
2 Antworten
+ 8
//try this
int num = Convert.ToInt32(Console.ReadLine());
int res = 1;
while (num>=res)
{
if(res%2==0){
Console.WriteLine(res);
}
res++;
}
0
Hi, can you explain me in more details please ?
I don't understand at all what's going on !