Условия и решения на задачите от лекция „Conditional Statements“
Задача 1. Compare Two Integers
Условие: Write an if statement that examines two integer variables and exchanges their values if the first one is greater than the second one.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _1.CompareTwoInteger { class Program { static void Main() { int a, b, c; Console.Write("Enter the first number a:"); bool isaInt = int.TryParse(Console.ReadLine(),out a); Console.Write("Enter the second number b:"); bool isbInt = int.TryParse(Console.ReadLine(),out b); if (isaInt && isbInt) { if (a > b) { c = a; a = b; b = c; } Console.WriteLine("a={0}",a); Console.WriteLine("b={0}",b); } else { Console.WriteLine("Not a valid entry! Some of the values are not integer!"); } } } }
Задача 2. Show The Sign Of The Product
Условие: Write a program that shows the sign (+ or -) of the product of three real numbers without calculating it. Use a sequence of if statements.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Globalization; namespace _2.ShowTheSignOfTheProduct { class Program { static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; double a, b, c; byte positiveNumbers=0; Console.Write("Enter the first number a:"); bool isaDouble = double.TryParse(Console.ReadLine(), out a); Console.Write("Enter the second number b:"); bool isbDouble = double.TryParse(Console.ReadLine(), out b); Console.Write("Enter the third number c:"); bool iscDouble = double.TryParse(Console.ReadLine(), out c); if (isaDouble && isbDouble && iscDouble) { if (Math.Sign(a) == 1) { positiveNumbers=(byte)(positiveNumbers + 1); } if (Math.Sign(b) == 1) { positiveNumbers = (byte)(positiveNumbers + 1); } if (Math.Sign(c) == 1) { positiveNumbers = (byte)(positiveNumbers + 1); } if (positiveNumbers % 2 == 0) { Console.WriteLine("The sign of the product is (-)"); } else { Console.WriteLine("The sign of the product is (+)"); } } else { Console.WriteLine("Not a valid entry! Some of the values are not double!"); } } } }
Задача 3. Find The Biggest Number Of Three
Условие: Write a program that finds the biggest of three integers using nested if statements.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _3.FindTheBiggestNumberOfThree { class Program { static void Main() { int a, b, c,biggestNumber; Console.Write("Enter the first number a:"); bool isaInt = int.TryParse(Console.ReadLine(), out a); Console.Write("Enter the second number b:"); bool isbInt = int.TryParse(Console.ReadLine(), out b); Console.Write("Enter the third number c:"); bool iscInt = int.TryParse(Console.ReadLine(), out c); if (isaInt && isbInt && iscInt) { if (a > b) { if (b > c) { biggestNumber = a; } else { if (c > a) { biggestNumber = c; } else { biggestNumber = a; } } } else { if (a > c) { biggestNumber = b; } else { if (c > b) { biggestNumber = c; } else { biggestNumber = b; } } } Console.WriteLine("The biggest number={0}", biggestNumber); } else { Console.WriteLine("Not a valid entry! Some of the values are not integer!"); } }
Задача 4. Sort 3 Real Numbers
Условие: Sort 3 real values in descending order using nested if statements.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Globalization; namespace _4.Sort3RealNumbers { class Program { static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; double a, b, c; double[] array={0,0,0}; Console.Write("Enter the first number a:"); bool isaDouble = double.TryParse(Console.ReadLine(), out a); Console.Write("Enter the second number b:"); bool isbDouble = double.TryParse(Console.ReadLine(), out b); Console.Write("Enter the third number c:"); bool iscDouble = double.TryParse(Console.ReadLine(), out c); if (isaDouble && isbDouble && iscDouble) { if (a > b) { if (b > c) { array[0] = a; array[1] = b; array[2] = c; } else { if (c > a) { array[0] = c; array[1] = a; array[2] = b; } else { array[0] = a; array[1] = c; array[2] = b; } } } else { if (a > c) { array[0] = b; array[1] = a; array[2] = c; } else { if (c > b) { array[0] = c; array[1] = b; array[2] = a; } else { array[0] = b; array[1] = c; array[2] = a; } } } Console.WriteLine("The descending order is {0};{1};{2}", array[0],array[1],array[2]); } else { Console.WriteLine("Not a valid entry! Some of the values are not integer!"); } } } }
Задача 5. Print Name Of The Digit
Условие: Write program that asks for a digit and depending on the input shows the name of that digit (in English) using a switch statement.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _5.PrintNameOfTheDigit { class Program { static void Main() { byte digit; Console.Write("Enter the digit:"); bool isDigit = byte.TryParse(Console.ReadLine(), out digit); if (isDigit) { switch (digit) { case 0: Console.WriteLine("zero"); break; case 1: Console.WriteLine("one"); break; case 2: Console.WriteLine("two"); break; case 3: Console.WriteLine("three"); break; case 4: Console.WriteLine("four"); break; case 5: Console.WriteLine("five"); break; case 6: Console.WriteLine("six"); break; case 7: Console.WriteLine("seven"); break; case 8: Console.WriteLine("eight"); break; case 9: Console.WriteLine("nine"); break; default: Console.WriteLine("This is not one digit entry!"); break; } } else { Console.WriteLine("Not a valid entry! This is not one digit entry!"); } } } }
Задача 6. Qadratic Equation
Условие: Write a program that enters the coefficients a, b and c of a quadratic equation a*x2 + b*x + c = 0 and calculates and prints its real roots. Note that quadratic equations may have 0, 1 or 2 real roots.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Globalization; namespace _6.QadraticEquation { class Program { static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; double a, b, c, discriminant, x1, x2; Console.Write("Enter the first coefficient a:"); bool isaDouble = double.TryParse(Console.ReadLine(), out a); Console.Write("Enter the second coefficients b:"); bool isbDouble = double.TryParse(Console.ReadLine(), out b); Console.Write("Enter the third coefficients c:"); bool iscDouble = double.TryParse(Console.ReadLine(), out c); if (isaDouble && isbDouble && iscDouble) { discriminant = b * b - 4 * a * c; if (discriminant > 0) { x1 = (-b + Math.Sqrt(discriminant)) / 2 * a; x2 = (b + Math.Sqrt(discriminant)) / 2 * a; Console.WriteLine("The real roots are:"); Console.WriteLine("x1={0}", x1); Console.WriteLine("x2={0}", x2); } else if (discriminant == 0) { x1 = x2 = -b / 2 * a; Console.WriteLine("The real roots are:"); Console.WriteLine("x1={0}", x1); Console.WriteLine("x2={0}", x2); } else { Console.WriteLine("There are no real roots!"); } } else { Console.WriteLine("Not a valid entry! Some of the numbers are not double!"); } } } }
Задача 7. Find The Greatest From 5 Variables
Условие: Write a program that finds the greatest of given 5 variables.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Globalization; namespace _7.FindTheGreatestFrom5Variables { class Program { static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; double[] array={0,0,0,0,0}; bool isNumber=true; double greatest=double.MinValue; for (int i = 0; i < 5; i++) { Console.Write("Enter the {0} variable:",i+1); isNumber = double.TryParse(Console.ReadLine(), out array[i]); if(isNumber==false) { Console.WriteLine("Not a valid entry! This is not a number entry!"); break; } else { if(array[i] > greatest) { greatest = array[i]; } } } if (isNumber) { Console.WriteLine("The greatest number = {0}", greatest); } } } }
Задача 8. Int Double Or String Conversion
Условие: Write a program that, depending on the user’s choice inputs int, double or string variable. If the variable is integer or double, increases it with 1. If the variable is string, appends „*“ at its end. The program must show the value of that variable as a console output. Use switch statement.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Globalization; namespace _8.IntDoubleOrStringConversion { class Program { static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; double doubleNumber=0; int intNumber=0; string stringInput; byte entryType; Console.Write("Please do the data entry:"); stringInput = Console.ReadLine(); bool isDouble = double.TryParse(stringInput,out doubleNumber); if (isDouble) { if (doubleNumber % 1 == 0) { intNumber = (int)doubleNumber; entryType = 1;//Integer entry } else { entryType = 2;//Double entry } } else { entryType = 3;//String entry } switch (entryType) { case 1: Console.WriteLine("Integer input. The output is:{0}", intNumber + 1); break; case 2: Console.WriteLine("Double input. The output is:{0}", doubleNumber + 1); break; case 3: Console.WriteLine("String input. The output is:{0}", stringInput + "*"); break; } } } }
Задача 9. Check If The Sum Of Subset Is 0
Условие: We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0. Example: 3, -2, 1, 1, 8 => 1+1-2=0.
Решение:
using System; using System.Collections.Generic; namespace _9.CheckIfTheSumOfSubsetIs0 { class Program { static void Main() { bool isInt=true; bool isElementZero=false; int[] intNumbers=new int[5]; Console.Write("Enter a sequence of five integers delimited with \",\":"); string[] strNumbers =(Console.ReadLine()).Split(','); if (strNumbers.Length == 5) { for (int i = 0; i < 5; i++) { isInt = int.TryParse(strNumbers[i], out intNumbers[i]); if (isInt == false) { break; } if (intNumbers[i]==0) { isElementZero = true; } } if (isInt) { if (isElementZero) { Console.WriteLine("There is a subset with sum equal to 0"); } else { switch (FindZeroSum(intNumbers)) { case true: Console.WriteLine("There is a subset with sum equal to 0"); break; case false: Console.WriteLine("There is no subset with sum equal to 0"); break; } } } else { Console.WriteLine("Wrong entry! Some of the numbers are not integer!"); } } else { Console.WriteLine("Wrong entry! The members of the sequence are less or more than 5!"); } } private static bool FindZeroSum(int[] numbers) { int[] dynamicSums = new int[32]; for (int i = 1; i < 5; i++)// numbers in the array { int j, k; for (j = 0; j < ((1 << i) - (i + 1)); j++)// the sum with already summed elements { if ((dynamicSums[(1 << i) - (i + 1) + j] = numbers[i] + dynamicSums[j]) == 0) { return true; } } for (k = 0; k < i; k++)// the sums with the new elements { if ((dynamicSums[(1 << i) - (i + 1) + (j + k)] = numbers[k] + numbers[i]) == 0) { return true; } } } return false; } } }
Задача 10. Bonus Scores
Условие: Write a program that applies bonus scores to given scores in the range [1..9]. The program reads a digit as an input. If the digit is between 1 and 3, the program multiplies it by 10; if it is between 4 and 6, multiplies it by 100; if it is between 7 and 9, multiplies it by 1000. If it is zero or if the value is not a digit, the program must report an error.
Use a switch statement and at the end print the calculated new value in the console.
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _10.BonusScores { class Program { static void Main() { int n; Console.Write("Enter a digit in the range of [1-9]:"); bool isnInt = int.TryParse(Console.ReadLine(),out n); if (isnInt && n > 0 && n < 10) { switch (n) { case (1): n = n * 10; break; case (2): n = n * 10; break; case (3): n = n * 10; break; case (4): n = n * 100; break; case (5): n = n * 100; break; case (6): n = n * 100; break; case (7): n = n * 1000; break; case (8): n = n * 1000; break; case (9): n = n * 1000; break; } Console.WriteLine(n); } else { Console.WriteLine("Not a valid entry!"); } } } }
Задача 11. Number To Text Converter
Условие: Write a program that converts a number in the range [0…999] to a text corresponding to its English pronunciation. Examples:
0 => „Zero“
273 => „Two hundred seventy three“
400 => „Four hundred“
501 => „Five hundred and one“
711 => „Seven hundred and eleven“
Решение:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _11.NumberToTextConverter { class Program { static void Main(string[] args) { int number; bool isNumberInt; string[,] numbers ={ {"one","two","three","four","five","six","seven","eight","nine"}, {"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}, {"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"}, }; Console.WriteLine("Enter the number:"); isNumberInt = int.TryParse(Console.ReadLine(), out number); if (isNumberInt && number > -1 && number < 1000) { if (number / 100 > 0)//Are there any hundreds? { Console.Write("{0} hundred ", numbers[0, (number / 100) - 1]); if ((number % 100) / 10 > 0)//Are there any tens? { Console.Write("and "); if ((number % 100) / 10 == 1 && (number % 100) % 10 > 0)//Are the tens between 10 and 20? { Console.WriteLine(numbers[1, ((number % 100) % 10) - 1]); } else { Console.Write(numbers[2, ((number % 100) / 10) - 1]); if ((number % 100) % 10 > 0)//Are there any units? { Console.WriteLine(" {0}", numbers[0, ((number % 100) % 10) - 1]); } else { Console.WriteLine(); } } } else if (((number % 100) % 10) > 0)//Are there any units? { Console.Write("and "); Console.WriteLine(numbers[0, ((number % 100) % 10) - 1]); } else { Console.WriteLine(); } } else if (number / 10 > 0 && number / 10 < 10)//Are there only tens and units? { if (number / 10 == 1 && number % 10 > 0)//Are the tens between 10 and 20? { Console.WriteLine(numbers[1, ((number % 100) % 10) - 1]); } else { Console.Write(numbers[2, (number / 10) - 1]); if (number % 10 > 0) { Console.WriteLine(" {0}", numbers[0, ((number % 100) % 10) - 1]); } else { Console.WriteLine(); } } } else if (number == 0)//Is the number 0? { Console.WriteLine("zero"); } else//Are there only units? { Console.WriteLine(numbers[0, number - 1]); } } else { Console.WriteLine("Not a valid entry!"); } } } }