C# Tutorial Part 4.2 – Operators Questions

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;
     B -=A;
     Console.WriteLine(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:

Answers:

1) %
2) 3
3) Assignment Operator
4) -18
5) 4294967278
6) c
7) 105
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.

A C# Program which takes two values and performs a series of operations on them, outputting the results.
That’s operators finished, next up is selection in Part 5.0!

Advertisements

About Matt

I like writing, filmmaking, programming and gaming, and prefer creating media to consuming it. On the topic of consumption, I'm also a big fan of eating.
This entry was posted in Programming and tagged , , , , , , , . Bookmark the permalink.

Enter comment:

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s