0

It's not executing and I dont know why.

int num = Convert.ToInt32(Console.ReadLine()); int res = 1; //your code goes here while(num < 9) { Console.WriteLine(num);num+=2; }

3rd Jan 2021, 7:35 PM
Swiff _Vastolorde
Swiff _Vastolorde - avatar
6 ответов
+ 2
Since it is clearly stated that "... from 1 to N inclusive ..." we can initialize <res> by 2 (the nearest even number greater than 1), and then increment <res> by 2 in the while-loop body. And again, by the "inclusive" specific, we'd use `while( res <= num)` in order to also include <num> in outputs.
3rd Jan 2021, 8:22 PM
Ipang
+ 3
What is the instruction? If given input value was greater or equal to 9, the loop will not start, simply because the loop condition isn't satisfied.
3rd Jan 2021, 8:12 PM
Ipang
+ 2
Alright man, doing good job! Keep it up 👍
4th Jan 2021, 1:28 AM
Ipang
+ 1
Ipang 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
3rd Jan 2021, 8:14 PM
Swiff _Vastolorde
Swiff _Vastolorde - avatar
+ 1
Ipang you the man bro. I really appreciate the help. It worked. int num = Convert.ToInt32(Console.ReadLine()); int res = 2; //your code goes here while(res <= num) { Console.WriteLine(res);res+=2; }
3rd Jan 2021, 8:31 PM
Swiff _Vastolorde
Swiff _Vastolorde - avatar
0
Ipang I'm having trouble with this. It's a for loop. I'm confused on calculating the sum. The program you are given takes a positive number N as input. Complete the program to calculate the sum of all numbers from 1 to N inclusive. Sample Input 4 Sample Output 10
4th Jan 2021, 7:25 PM
Swiff _Vastolorde
Swiff _Vastolorde - avatar