0% found this document useful (0 votes)
3 views

Lab02 C#

lab02 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab02 C#

lab02 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

204111 – Computers and Programming Section 451

Lab #2 – C Basic (Part II)

Student ID Name Signature


Sheet’s Owner
Group partner

1. Math Class Methods

In addition to the arithematic operators (+ - * /), C# provides you with a collection of math
functions inside the Math class, as the following:

Method Value Returned


Math.Abs(x) The absolute value of x, |x|
Math.Ceiling(x) Smallest integer greater than or equal to x
Math.Floor(x) Largest integer smaller than or equal to x
Math.Log(x) Natural logarithmic of x
Math.Log10(x) 10-base logarithmic of x
Math.Round(x) Nearest integer to x
Math.Pow(x, y) x raised to the y th power, xy

Math.Sqrt(x) Square-root of x, x
Math.Max(x, y) Larger of x and y
Math.Min(x, y) Smaller of x and y
Math.Sin(x) Sine of x radians
Math.Cos(x) Cosine of x radians
Constant Description
Math.PI The constant π which is approximately 3.14159265358979323846
Math.E The constant e which is approximately 2.7182818284590452354

Exercise 1.1: (Working with  ) Consider the following program

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 }

What is the output of the above program?

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.

Statement What is computed Result


j = Math.Abs(-1); |√
− 1| 1
j = Math.Sqrt(5); 5 2.2361
j = Math.Abs(9.5);
j = Math.Pow(5,2);
j = Math.Pow(2,5);
j = Math.Pow(2,-1);
j = Math.Pow(5,0.5);
j = Math.Sqrt(2);
j = Math.Log(0);
j = Math.Log(10);
j = Math.Log10(100);
j = Math.Log(2);

Lab #2 3 of 6
204111 – Computers and Programming Section 451

Exercise 1.7: (Mapping math formulas to C expressions) Convert the mathematic


expressions on the left column into a valid Cexpression. Then write the answer
in the right column.

Math Expression C Expression


xy + z Math.Pow(x,y) + z
cos(2π) + loge (x) Math.Cos(2*Math.PI)+Math.Log(x)
|x| + |y|

x2 + y 2 + z 2
sin2 (x) + cos2 (x)
√5
a+b
ex ln y

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

Enter the first number: 


Enter the second number: 
The number 80 is larger than 50.

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

Task 2.2: Quadratic Equation


A quadratic equation is of the form

ax2 + bx + c = 0,

where the roots x can be found using the quadratic formula:



−b ± b2 − 4ac
x= . (1)
2a
Your task is to write a program to solve a quadratic equation by taking the quadratic
coefficients (i.e., a, b, and c) from the user. The program then computes the roots x using the
formula shown in (1). Both roots must be reported even though they are the same.
Sample output

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

Task 2.3: Circle Properties


It is well known that a circle with a radius r has the area πr 2 and the circumference 2πr.

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

Enter the circumference:  !


The radius of the circle is 4.00
The area of the circle is 50.27

Once you are done with your program on SharpDevelop, write your code in the box below:

Lab #2 6 of 6

You might also like