- 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.
2 Answers
+ 1
read more about the operator %
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)