0

Write a program to take N numbers as input and output all even numbers from 1 to N inclusive, each on a new line.

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 My Code 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 = 1; //your code goes here while (num > res){Console.WriteLine(res); res++; if (res % 2 == 0) continue;} } } } Please someone help me with this.

26th Jun 2021, 2:09 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
11 Antworten
26th Jun 2021, 3:35 PM
hossein B
hossein B - avatar
+ 2
If you start from 2 instead of 1, you can escape the need of using modulo operator to check for even numbers. You can simply increment <res> by 2 on each loop round to get to the next even number.
26th Jun 2021, 5:05 PM
Ipang
+ 1
while (num >= res) { if (res % 2 == 0) Console.WriteLine(res); res++; }
26th Jun 2021, 2:13 PM
visph
visph - avatar
+ 1
I've managed to kinda solve the problem with this task I tried this and it "Solved" the challenge 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; //your code goes here while(res <= num) { Console.WriteLine(res); res+=2; } } } } Tell me what you think about my answer!
10th Jan 2022, 10:46 PM
Ivan Ivanov
Ivan Ivanov - avatar
0
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 = 1; //your code goes here int evennum = 2; while (num > res) { if (num%2 == 0) { Console.WriteLine(evennum); evennum+=2; } num--; } } } }
23rd Jul 2021, 5:28 PM
Anubis
0
I understand all of this except from the «num—« part. Why does it have to be there? And what does it mean?
23rd Jul 2021, 8:11 PM
Niklas H
Niklas H - avatar
0
Take a look at my first code and see the problem. 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 = 1; //your code goes here while (num > res) { if (num%2 == 0) { Console.WriteLine(num); } num--; } } } }
24th Jul 2021, 10:37 AM
Anubis
0
I hope that I am able to explain it in english 😅 Num— counting down the given number by 1. Take a look at lesson 10.1 Basic Concepts. For example: 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 = 1; //your code goes here while (num > res) { Console.WriteLine(num); num--; } } } } Whats the condition for the while loop ? Right num is bigger than res. So the loop will run as long this condition is true. The question now is: How we are able to stop the loop and only give out all even numbers?
24th Jul 2021, 11:04 AM
Anubis
0
To stop the loop we need to decrease the given number, everytime the loop is executing. For this I took num—. And then we only need to filter the even numbers: if (num%2 == 0) { Console.WriteLine(evennum); evennum+=2; } You wrote that you understand that part so no more explanation. 😅 Why I used a third variable? It solved my problem of counting down instead of counting up. Compare my codes. Kind regards.
24th Jul 2021, 11:04 AM
Anubis
0
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 = 1; //your code goes here while (res < num) { if (res % 2 == 0) { Console.WriteLine(res); } res ++; } } } }
23rd Jan 2022, 9:08 PM
Adrian Stan
- 1
i=int(input('Enter a number:')) even=list(filter(lambda x:x%2==0,list(range(1,i)))) for even in even: print() print(even)
26th Jun 2021, 4:27 PM
Lil Code X
Lil Code X - avatar