0
The while Loop
Hello, I cannot make a correct code the content of the task: 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
5 Respuestas
+ 2
you must iterate from 1 (res) to num, and write the variable used to iterate...
I rather would do it with a 'for' loop than with a while loop (even if both are possible), using res as iteration variable:
for (int res=1; res<=num; ++res)
+ 3
with while loop, you need to declare and assign res outside and before the loop, then you must to check while (res<=num), and inside the loop body you need to print 'if (res%2==0)' to print res, and not forget to increment res at end of loop body ;)
+ 1
Omg, it was so simpleeee... Thank you a lot!
0
For me the for seems to be a simpler loop, but this task is in "The while loop".
I cannot understand it, though it is simple
- 1
That's my code:
using System;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int num = Convert.ToInt32(Console.ReadLine());
int res = 1;
while(num % 2 == 0)
{
Console.WriteLine(??);
}
}
}
"??" I'm not sure what should be there