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
xand set its value to0 - Use an
ifstatement to check ifxis even- Use the
%operator to accomplish this: if any number%2 equals 0, that means it is even
- Use the
- If
xis 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
ifstatement, create thewhileloopwhilekeyword- Parentheses
() - Curly brackets
{}
- Within the parentheses, use a condition to make sure the count is capped at 100
- HINT: Compare
xto100in some way
- HINT: Compare
- In the body of the
whileloop (between the curly braces), increment the value ofxby 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
ifcode that checks a number's evenness- Include the
if, and the body that prints to the console
- Include the
- Paste the
ifcode within thewhileloop- 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.