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.
11 Antworten
+ 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.
+ 1
while (num >= res) {
if (res % 2 == 0) Console.WriteLine(res);
res++;
}
+ 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!
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--;
}
}
}
}
0
I understand all of this except from the «num—« part. Why does it have to be there? And what does it mean?
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--;
}
}
}
}
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?
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.
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 ++;
}
}
}
}
- 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)