Lab02 C#
Lab02 C#
In addition to the arithematic operators (+ - * /), C# provides you with a collection of math
functions inside the Math class, as the following:
1 System ;
2 TestMath {
3 Main () {
4 j ;
5 ___________________________
6 Console . W r i t e L i n e( j );
7 Console . ReadLine ();
8 }
9 }
Using the table below, fill in the blank at line 5 with the statements provided in the left column
of the table, one at a time, then write the results from the program in the right column.
Lab #2 1 of 6
204111 – Computers and Programming Section 451
Statement Result
j = Math.Floor(1.1); 1
j = Math.Floor(1.2);
j = Math.Floor(1.3);
j = Math.Floor(1.4);
j = Math.Floor(1.5);
j = Math.Floor(1.6);
j = Math.Floor(1.7);
j = Math.Floor(1.8);
j = Math.Floor(1.9);
j = Math.Floor(2.0);
Exercise 1.2: (Working with ) Using the program from Exercise 1.1,
fill in the blank with the statements on the left column of the table below, one a
time. Write the results in the right column.
Statement Result
j = Math.Round(1.1); 1
j = Math.Round(1.2);
j = Math.Round(1.3);
j = Math.Round(1.4);
j = Math.Round(1.5);
j = Math.Round(1.6);
j = Math.Round(1.7);
j = Math.Round(1.8);
j = Math.Round(1.9);
j = Math.Round(2.0);
Exercise 1.3: (Working with ) Using the program from Exercise 1.1,
fill in the blank with the statements on the left column of the table below, one a
time. Write the results in the right column.
Statement Result
j = Math.Ceiling(1.1); 2
j = Math.Ceiling(1.2);
j = Math.Ceiling(1.3);
j = Math.Ceiling(1.4);
j = Math.Ceiling(1.5);
j = Math.Ceiling(1.6);
j = Math.Ceiling(1.7);
j = Math.Ceiling(1.8);
j = Math.Ceiling(1.9);
j = Math.Ceiling(2.0);
Lab #2 2 of 6
204111 – Computers and Programming Section 451
Exercise 1.4: From Exercises 1.1, 1.2, and 1.3, describe the differences among the
methods , and below.
Exercise 1.5: (Working with and ) Consider the following
program:
1 MinMax {
2 Main () {
3 num = System . Math . Min (6 , 20);
4 System . Console . W r i t e L i n e( " Result = {0} " , num );
5 }
6 }
Now change Math.Min in line 3 into Math.Max instead. Run the program and write the
result in the box below.
Exercise 1.6: (Working with other math functions) Write a small program to de-
termine the value of the variable j in each of the statements listed in the table
below. Write the values of j in the right column and what each statement intends
to compute in the middle column.
Lab #2 3 of 6
204111 – Computers and Programming Section 451
2. Programming Tasks
Task 2.1: Write a C program which takes two input numbers from the user, then
determines which number is larger and which is smaller (assuming the two numbers
are always different).
Sample output
Note that the numbers 50 and 80 in the above example are entered by the user. Copy your
program into the box provided below:
Lab #2 4 of 6
204111 – Computers and Programming Section 451
ax2 + bx + c = 0,
Enter a:
Enter b:
Enter c:
x = 2.5, -4
Once you are done with your program on SharpDevelop, write your code in the box below:
Lab #2 5 of 6
204111 – Computers and Programming Section 451
Write a program that takes the circumference of a circle from the user, then computes the
circle’s radius and area. All results must be shown with two decimal places.
Sample output
Once you are done with your program on SharpDevelop, write your code in the box below:
Lab #2 6 of 6