0
Please help
Can someone help me with this? I am stuck on it for a while now: Write a program to take N number as input and output all even numbers from 1 to N inclusive, each on a new line. static void Main(string[] args) { int num = Convert.ToInt32(Console.ReadLine()); int res = 1; //your code goes here }
5 Respostas
+ 6
Well, you don't seem to be making use of res. You should be using that as index instead of num. The num variable represents the largest value you need to evaluate, and that should not change throughout the execution of the program.
Apart from that, your line of logic is fairly interesting. Usually, what people would do is to evaluate the numbers one by one and output those which are even. Consider:
while (res <= num)
{
if (res % 2 == 0)
Console.WriteLine(res);
res += 1;
}
but seems like you plan to just add 2 to an already known even value, and output everything up till N. For that, the proper way of representing it in code would be:
// Start from 2 since 1 is odd
res = 2;
while (res <= num)
{
Console.WriteLine(res);
res += 2;
}
+ 5
If you've been stuck on this for a while, you'd probably have written at least a loop and some conditional statements? Can you show us those attempts?
+ 3
Have you got to the chapter covering the use of loops? it would help a lot to understand how to use loops here cause use of a loop is the basic idea behind a solution for tasks like these.
+ 2
you have to use -
hint : loops , if condition
+ 2
int num = Convert.ToInt32(Console.ReadLine());
int res = 1;
//your code goes here
num=num/num*2;
while (num>0)
{
Console.WriteLine(num);
num+=2;
}
This is the closest I got