Q) When is 1 divided by 3 = 0?

A) When you are doing integer division in C#.

Dividing two integers always produces an integer. That is why 1/3 = 0.  You can either cast the integers into doubles (in the number2 example) or use doubles instead of integers (number3 example).

double number = 1 / 3;
double number2 = (double)1 / (double)3;
double number3 = 1.0 / 3.0;
TaskDialog.Show("Division","Your results are:\n" + number + "\n" + number2 + " \n" + number3);

divisionl

 


		
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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