C# Practice Exercises part 3

2
Practice Exercises – Boolean Algebra, Complex Decisions, Loops, Flowcharts 1 1. Write a program that finds the maximum value among three numbers (using three variables, not an array). 2. Write a program that checks if a number entered by a user is a prime number (a prime number is divisible only by 1 and by itself) Hint: to check if the remainder of division between two numbers is 0, use the modulo operator % as in the example below: number % 2 == 0 where number is an integer variable). 3. To practice analyzing Boolean expressions, write the truth tables for the following propositions: a) p ^ ~q b) ~(p v ~q) c) ~(p ^ q) d) ~p v ~q e) ~(p v q) ^ r f) p v q v ~r 4. You are a man who proposes to his girlfriend; however, because your girlfriend is a very rich and spoiled girl, she says: “I will marry you, but under certain conditions: each month, we’ll go shopping between the 15 th and 18 th and we’ll visit my parents between the 25 th and 27 th . My brother is a bad student, so you will be his tutor each month between the 5 th and the 14 th . Finally, I will ask my father to give you a job in his company, so you will have to work during the other days.” Write a program that asks the user to enter a day and outputs the activity that should be performed on that day, depending on the girl’s requests. 5. Write a program that prints “I’m a monkey, I’m a donkey” until the user enters the number 5.

description

C# Practice Exercises part 3

Transcript of C# Practice Exercises part 3

Page 1: C# Practice Exercises part 3

Practice Exercises – Boolean Algebra, Complex Decisions, Loops, Flowcharts

1

1. Write a program that finds the maximum value among three numbers (using three

variables, not an array).

2. Write a program that checks if a number entered by a user is a prime number (a prime

number is divisible only by 1 and by itself) Hint: to check if the remainder of division

between two numbers is 0, use the modulo operator % as in the example below:

number % 2 == 0

where number is an integer variable).

3. To practice analyzing Boolean expressions, write the truth tables for the following

propositions:

a) p ^ ~q

b) ~(p v ~q)

c) ~(p ^ q)

d) ~p v ~q

e) ~(p v q) ^ r

f) p v q v ~r

4. You are a man who proposes to his girlfriend; however, because your girlfriend is a very

rich and spoiled girl, she says: “I will marry you, but under certain conditions: each month,

we’ll go shopping between the 15th and 18th and we’ll visit my parents between the 25th

and 27th. My brother is a bad student, so you will be his tutor each month between the 5th

and the 14th. Finally, I will ask my father to give you a job in his company, so you will have

to work during the other days.”

Write a program that asks the user to enter a day and outputs the activity that should be

performed on that day, depending on the girl’s requests.

5. Write a program that prints “I’m a monkey, I’m a donkey” until the user enters the

number 5.

Page 2: C# Practice Exercises part 3

Practice Exercises – Boolean Algebra, Complex Decisions, Loops, Flowcharts

2

6. Write a function that takes an array as parameter and calculates the arithmetic mean of

the array elements. The arithmetic mean is calculated using the following equation:

where n is the number of elements in the array and xi is the ith array element. To test your

function, fill an array with few numbers, print the returned result on screen, and compare it

with a result calculated using a calculator.

Hint: make an array of type double because if you use an integer array and then divide

integers, you will not get the correct result; note that this is something that we haven’t

talked about during the lectures.

7. In the last lecture, we had an example in which we were looking for the largest element

in an integer array. Modify that example to:

Find the smallest value in an array.

Find the position of the smallest value.