0

The while Loop

Hello, I cannot make a correct code the content of the task: Write a program to take N numbers as input and output all even numbers from 1 to N inclusive, each on a new line. Sample Input 9 Sample Output 2 4 6 8

22nd Mar 2021, 4:01 PM
Sebastian Górski
Sebastian Górski - avatar
5 ответов
+ 2
you must iterate from 1 (res) to num, and write the variable used to iterate... I rather would do it with a 'for' loop than with a while loop (even if both are possible), using res as iteration variable: for (int res=1; res<=num; ++res)
22nd Mar 2021, 4:14 PM
visph
visph - avatar
+ 3
with while loop, you need to declare and assign res outside and before the loop, then you must to check while (res<=num), and inside the loop body you need to print 'if (res%2==0)' to print res, and not forget to increment res at end of loop body ;)
22nd Mar 2021, 4:21 PM
visph
visph - avatar
+ 1
Omg, it was so simpleeee... Thank you a lot!
22nd Mar 2021, 4:29 PM
Sebastian Górski
Sebastian Górski - avatar
0
For me the for seems to be a simpler loop, but this task is in "The while loop". I cannot understand it, though it is simple
22nd Mar 2021, 4:18 PM
Sebastian Górski
Sebastian Górski - avatar
- 1
That's my code: using System; namespace SoloLearn { class Program { static void Main(string[] args) { int num = Convert.ToInt32(Console.ReadLine()); int res = 1; while(num % 2 == 0) { Console.WriteLine(??); } } } "??" I'm not sure what should be there
22nd Mar 2021, 4:02 PM
Sebastian Górski
Sebastian Górski - avatar