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; }
6 Réponses
+ 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.
+ 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.
+ 2
Alright man, doing good job!
Keep it up 👍
+ 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
+ 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;
}
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