Thursday, March 27, 2008
C# Programming - Part 2 - Conditions
Scope of work
The work is split into two parts: the first is a training part where you are encouraged to use books and the Internet to help you complete the questions; the second is a test part where you should complete the questions with no external help. The pass score is 80%.
1. if, else if, else
2. Nested conditions
3. && and
4. switch statement
Training Questions
Create console applications to:
1. Read a number from the console (use ReadLine and Int32.Parse) and using if, else if and else output one of the following
“The number is negative”
“The number is lower than 100”
“The number is 100 or above”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“The numbers are the same”
“The numbers are not the same”
– the first is lower than the second”
“The numbers are not the same
– the second is lower than the first”
3. Read two numbers from the console (one after the other) and using if, else if and else along with && output one of the following:
“The numbers are the same and are negative”
“The numbers are the same and are positive”
“The numbers are not the same”
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-10 output one of the following:
“You entered an even number” or
“You entered an odd number” or
“You entered a number that is not 1-10”
5. Read a number from the console (use ReadLine and Int32.Parse) and by nesting if, else if and else output one of the following depending on what range the number is in, and whether it is divisible by 5:
“The number is 0-25 and is divisible by 5” or
“The number is 0-25 and is not divisible by 5” or
“The number is 26-50 and is divisible by 5” or
“The number is 26-50 and is not divisible by 5” or
“The number is 51-75 and is divisible by 5” or
“The number is 51-75 and is not divisible by 5” or
“The number is above 75”
* Note. To check if a number is divisible by 5 you can use:
(number % 5 == 0)
Test Questions
Create console applications to:
1. Read two numbers from the console (one after the other) and using if, else if and else output one of the following:
“The first number is lower than the second”
“The second number is lower than the first”
“The numbers are the same”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“Both numbers are negative”
”Both numbers are positive”
“The first number is positive, the second is negative”
“The second number is positive, the first is negative”
3. Read a letter from the console and by if, else if and else with (or) output one of the following:
“1” (if the letter is a, b or c)
“2” (if the letter is d, e or f)
“3” (if the letter is g, h or i)
“4” (if the letter is j, k or l)
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-9 output one of the following:
“You entered the number one, two or three” or
“You entered the number four, five or six” or
“You entered the number seven, eight or nine” or
“You entered a number that is not 1-9”
5. Read three numbers from the console (one after the other) and by nesting if, else if and else sort the three numbers and output them in order:
Example: input numbers: 2 5 and 1
Output: 1, 2, 5
Example: input numbers 100, 45, 30
Output: 30, 45, 100
The work is split into two parts: the first is a training part where you are encouraged to use books and the Internet to help you complete the questions; the second is a test part where you should complete the questions with no external help. The pass score is 80%.
1. if, else if, else
2. Nested conditions
3. && and
4. switch statement
Training Questions
Create console applications to:
1. Read a number from the console (use ReadLine and Int32.Parse) and using if, else if and else output one of the following
“The number is negative”
“The number is lower than 100”
“The number is 100 or above”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“The numbers are the same”
“The numbers are not the same”
– the first is lower than the second”
“The numbers are not the same
– the second is lower than the first”
3. Read two numbers from the console (one after the other) and using if, else if and else along with && output one of the following:
“The numbers are the same and are negative”
“The numbers are the same and are positive”
“The numbers are not the same”
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-10 output one of the following:
“You entered an even number” or
“You entered an odd number” or
“You entered a number that is not 1-10”
5. Read a number from the console (use ReadLine and Int32.Parse) and by nesting if, else if and else output one of the following depending on what range the number is in, and whether it is divisible by 5:
“The number is 0-25 and is divisible by 5” or
“The number is 0-25 and is not divisible by 5” or
“The number is 26-50 and is divisible by 5” or
“The number is 26-50 and is not divisible by 5” or
“The number is 51-75 and is divisible by 5” or
“The number is 51-75 and is not divisible by 5” or
“The number is above 75”
* Note. To check if a number is divisible by 5 you can use:
(number % 5 == 0)
Test Questions
Create console applications to:
1. Read two numbers from the console (one after the other) and using if, else if and else output one of the following:
“The first number is lower than the second”
“The second number is lower than the first”
“The numbers are the same”
2. Read two numbers from the console (one after the other) and by nesting if, else if and else output one of the following:
“Both numbers are negative”
”Both numbers are positive”
“The first number is positive, the second is negative”
“The second number is positive, the first is negative”
3. Read a letter from the console and by if, else if and else with (or) output one of the following:
“1” (if the letter is a, b or c)
“2” (if the letter is d, e or f)
“3” (if the letter is g, h or i)
“4” (if the letter is j, k or l)
4. Read a number from the console (use ReadLine and Int32.Parse) and using a switch statement with cascading, and depending on an input 1-9 output one of the following:
“You entered the number one, two or three” or
“You entered the number four, five or six” or
“You entered the number seven, eight or nine” or
“You entered a number that is not 1-9”
5. Read three numbers from the console (one after the other) and by nesting if, else if and else sort the three numbers and output them in order:
Example: input numbers: 2 5 and 1
Output: 1, 2, 5
Example: input numbers 100, 45, 30
Output: 30, 45, 100
Subscribe to Posts [Atom]