+ 3
How do I Write a program to take N numbers as input and output all even numbers from 1 to N inclusive in C#
Iām having difficulty with this question. I have the first part down as Int num = Convert.ToInt32(Console.ReadLine()); While (num <= 10) { Console.WriteLine(num); num ++; } Iām suppose to make the console output it in even numbers starting from 1 but Iām confused with the examples and in my opinion they donāt show you how to do it, so Iāve been stumped for a few days. Any help is appreciated. Thank you in advance.
5 Answers
+ 7
You should create a new variable called curr and set it initially to 1. Every step, you print the value and then increment. Repeat until you printed the number you got as input.
+ 5
//You could try this:
int num = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= num; i++) {
if (i % 2 == 0) {
Console.WriteLine(i);
}
}
//For every number from one to num, if the number is divisible to 2, (if it is even), output the current number.
//Hope this helps!!
+ 1
can you show me an example how that would be written out?
+ 1
thank you for all the advice eveyone. much appreciated!