Even Numbers - Individual Exercise
Start by creating a new .cs
file/project. Then, follow the instructions below to create a program that prints some even numbers.
Part 1 - Starting from 0
- Declare a new variable
x
and set its value to0
- Use an
if
statement to check ifx
is even- Use the
%
operator to accomplish this: if any number%
2 equals 0, that means it is even
- Use the
- If
x
is even, print it to the console
At the end of this part, the program should print 0
to the console (it is already known that x
is even)
Part 2 - While Loop
Instead of simply using 0
, the program should iterate through 0, 1, 2, 3, ..., all the way to 100.
- Above the
if
statement, create thewhile
loopwhile
keyword- Parentheses
()
- Curly brackets
{}
- Within the parentheses, use a condition to make sure the count is capped at 100
- HINT: Compare
x
to100
in some way
- HINT: Compare
- In the body of the
while
loop (between the curly braces), increment the value ofx
by 1
At the end of this part, the loop should execute 101 times... but it's missing something
Part 3 - Putting it all together
The last step is to put the check for evenness within the body of the while
!
- Copy the
if
code that checks a number's evenness- Include the
if
, and the body that prints to the console
- Include the
- Paste the
if
code within thewhile
loop- Make sure it comes before the increment of
x
- Make sure it comes before the increment of
Now, the program should print out each even number: 0, 2, 4, 6, ..., 100!
BONUS - User Input
Lastly, instead of using 100
as the cap, the program should ask the user which number to use.