While loop c# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

While loop c#

Hi there, here is my problematic question that has had me stumped for days. 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 When I start the question the example has this input already: Int = Convert.ToInt32(Console.ReadLine()); Int res = 1; What I have put so far is while (num>res) { res *=2; Console.WriteLine(res); } I am stumped and would like any type of hints or help. Thank you.

28th Jan 2021, 6:47 PM
Brent Tyson
Brent Tyson - avatar
2 Answers
+ 1
read more about the operator %
28th Jan 2021, 9:07 PM
0shibka
0shibka - avatar
0
First line of code is missing num, should be "Int num = ..." obviously Think again, what you have to do to get from one number to the next at each step 2->4->6->8 By res*=2 you multiply by 2 each time and get 2->4->8->16 Also think about where you have to start (what is the first output). Your loop condition is almost correct, just you have to include num, so that has to be num>=res (or res<=num to follow common convention)
28th Jan 2021, 6:57 PM
Benjamin Jürgens
Benjamin Jürgens - avatar