+ 2

C# While Loop 15.2 Practice

Please help. Im very new to coding. But while I put this code in, i get the output of 8,6,4,2 which are the numbers I am looking for, just flipped. Need it to be 2,4,6,8 (there are 2 other tests outputs as well that I have to generalize the input) But heres what I have currently, any suggestions would be great!!! :) 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-=2; }

20th May 2021, 4:42 AM
Tyler Hines
Tyler Hines - avatar
4 Respuestas
+ 3
Awesome! Thank you David!!!
21st May 2021, 1:31 AM
Tyler Hines
Tyler Hines - avatar
0
Tyler Hines The good news is you've been able to implement looping logic that involves reducing a value by 2 until you reach the break condition where the input value is no longer greater than the value 1. This is actually the reverse of what you want to do. So, let's attempt describing how you might flip some of your logic. 😉👌 First... your logic reduces the input value in `num` by increments of 2 until it falls below the value assigned to `res`, which is 1. How would you reverse this logic? [HINT] ---- Rather than changing the input value for `num`, focus on changing the value of `res`. ---- I'll provide additional questions in follow up comments to consider - in case you get stuck. Just avoid reviewing answers from others who might just post the solution. I think you can actually get to a solution on your own with just a little help. From this point, ignore my follow up posts that start with: [HINT] or [SPOILER ALERT] ... until you need the help. Feel free to ask questions along the way.
21st May 2021, 1:29 AM
David Carroll
David Carroll - avatar
0
[HINT #1] If changing the value of `res`, how would the break condition need to change? The current break condition: while(num > res) keeps looping while num is greater than value. Given num is 10, each loop condition will appear as follows: (num > res) (10 > 1 ) is true ( 8 > 1 ) is true ( 6 > 1 ) is true ( 4 > 1 ) is true ( 2 > 1 ) is true ( 0 > 1 ) is false => break loop How would this need to look if the logic was reversed?
21st May 2021, 1:45 AM
David Carroll
David Carroll - avatar
0
[HINT #2] If you're still struggling, try thinking about the how the values could change to reach the desired result, then think about the code to achieve that result. Example: Given the input is 10, each loop condition COULD appear as follows: ( 2 <= 10 ) is true ( 4 <= 10 ) is true ( 6 <= 10 ) is true ( 8 <= 10 ) is true (10 <= 10 ) is true (12 <= 10 ) is false => break loop Now... think about how the code would need to change to produce conditional checks like these in each loop. Rather than post another hint, I'll wait for your follow-up questions.
21st May 2021, 2:05 AM
David Carroll
David Carroll - avatar