Welcome to the third part of the fourth topic of my C# Language tutorial. As usual, at the end of each topic I post some questions and tasks to assess / re-enforce your understanding of the past few lessons. Also as usual, these questions are optional. I recommend you try question eight at least because it is good practice for writing a whole program.
Here we go:
1) What operator symbol do I use to work out the remainder of a division?
2) What is 23%5? Write a program to work it out if you can’t be bothered with the maths
3) Name this operator: ‘=’
4) Write a program to work out the value of ~17
5) What if the value 17 is stored in a uint variable?
6) If I have a boolean variable called daytime and want to flip its value, do I write
a) daytime != daytime;
b) daytime = daytime!;
c) daytime = !daytime;
d) !daytime = daytime;
7) What is the output of the following code? You may need some paper. No cheating by copying and pasting it into a program!
int A = 70, B = 35;
A += B;
B = A++;
A = –B;
A = B–;
B = ++A;
8 ) Write a program which asks the user to input two numbers and performs each of the following operations on them, outputting the results with suitable explanatory text. The output should stay on screen long enough to be read. Extra points for more efficient code!
The operations are: + – * / %
HINT: Use the method ‘Convert.ToInt16();’ to convert the input into a number.
Read on for the answers:
3) Assignment Operator
8 ) See below. It is actually possible to use less lines of code than that by reducing the amount of Console.WriteLine statements, but that would result in one very long line and a very wide picture below.