събота, 5 януари 2013 г.

C#: Conditional Statements

Ето ги и задачите от лекцията Conditional Statements. Задачите от домашните не са никак трудни като цяло, но има някои при които има повече писане. Последната задача ми отне малко повече време докато я напиша. Видео и материали от лекцията могат да бъдат намерени тук.

1. 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;

class ExchangeValuesOfTwoVariables
{
    static void Main()
    {
        Console.WriteLine("Enter a value for for the first variable: ");
        int firstValue = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter a value for the second variable:");
        int secondValue = int.Parse(Console.ReadLine());
        int maxValue = firstValue;
        if (firstValue > secondValue)
        {
            firstValue = secondValue;
            secondValue = maxValue;
            Console.WriteLine("First variable is equal to {0} and second variable is equal to {1}", firstValue, secondValue);
        }
        else if (firstValue == secondValue)
        {
            Console.WriteLine("The values are equal");
        }
        else
        {
            Console.WriteLine("Second value is bigger than first value");
        }
    }
}

2. 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;

class SignOfThreeRealNumbersProduct
{
    static void Main()
    {
        Console.WriteLine("Enter the first real number;");
        int firstNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second real number");
        int secondNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the third real number");
        int thirdNumber = int.Parse(Console.ReadLine());
        if (firstNumber < 0 && secondNumber < 0 && thirdNumber < 0)
        {
            Console.WriteLine("The sign of the product is '-'");
        }
        else if (firstNumber < 0 ^ secondNumber < 0 ^ thirdNumber < 0)
        {
            Console.WriteLine("The sign of the product is '-'");
        }
        else
        {
            Console.WriteLine("The sign of the product is '+'");
        }
    }
}

3. Write a program that finds the biggest of three integers using nested if statements.
using System;

class BiggestOfThreeNumbers
{
    static void Main()
    {
        Console.WriteLine("Enter the first number: ");
        int firstNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second number: ");
        int secondNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the third number: ");
        int thirdNumber = int.Parse(Console.ReadLine());
        if (firstNumber > secondNumber & firstNumber > thirdNumber)
        {
            Console.WriteLine("The biggest number is: {0}", firstNumber);
        }
        else
        {
            if (firstNumber < secondNumber & secondNumber > thirdNumber)
            {
                Console.WriteLine("The biggest number is: {0}", secondNumber);
            }
            else
            {
                Console.WriteLine("The biggest number is: {0}", thirdNumber);
            }
        }
    }
}

4. Sort 3 real values in descending order using nested if statements.
using System;

class SortingThreeRealValues
{
    static void Main()
    {                                                                           
        Console.WriteLine("Enter the first number: ");
        int firstNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second number: ");
        int secondNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the third number: ");
        int thirdNumber = int.Parse(Console.ReadLine());
        if (firstNumber < secondNumber)
        {
            if (secondNumber < thirdNumber)
            {
                Console.WriteLine("The order of the numbers is: {0} {1} {2}", firstNumber, secondNumber, thirdNumber);
            }
            else
            {
                if (firstNumber > thirdNumber)
                {
                    Console.WriteLine("The order of the numbers is: {0} {1} {2}", thirdNumber, firstNumber, secondNumber);
                }
                else
                {
                    Console.WriteLine("The order of the numbers is: {0} {1} {2}", firstNumber, thirdNumber, secondNumber);
                }
            }
        }
        if (firstNumber > secondNumber)
        {
            if (secondNumber > thirdNumber)
            {
                Console.WriteLine("The order of the numbers is: {0} {1} {2}", thirdNumber, secondNumber, firstNumber);
            }
            else
            {
                if (firstNumber > thirdNumber)
                {
                    Console.WriteLine("The order of the numbers is: {0} {1} {2}", secondNumber, thirdNumber, firstNumber);
                }
                else
                {
                    Console.WriteLine("The order of the numbers is: {0} {1} {2}", secondNumber, firstNumber, thirdNumber);
                }
            }
        }
    }
}

5. 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;

class NameOfADigit
{
    static void Main()
    {
        Console.WriteLine("Enter one digit: ");
        int digit = int.Parse(Console.ReadLine());
        switch (digit)
        {
            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("Eigth"); break;
            case 9: Console.WriteLine("Nine"); break;
            case 0: Console.WriteLine("Zero"); break;
            default: Console.WriteLine("Error"); break;
        }
    }
}

6. 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;

class QuadraticEquation
{
    static void Main()
    {
        Console.WriteLine("Enter value for 'a'");
        double a = double.Parse(Console.ReadLine());
        Console.WriteLine("Enter value for 'b'");
        double b = double.Parse(Console.ReadLine());
        Console.WriteLine("Enter value for 'c'");
        double c = double.Parse(Console.ReadLine());
        double d = b * b - (4 * a * c);
        Console.WriteLine("d = {0}" , d);
        if (d < 0)
        {
            Console.WriteLine("There are no real roots");
        }
        else if (d == 0)
        {
            double x = (-1 * b) / (2 * a);
            Console.WriteLine("x = {0}" , x);
        }
        else
        {
            double x1 = ((-1 * b) + Math.Sqrt(d)) / (2 * a);
            double x2 = ((-1 * b) - Math.Sqrt(d)) / (2 * a);
            Console.WriteLine("x1 = {0}" , x1);
            Console.WriteLine("x2 = {0}" , x2);
        }
    }
}

7. Write a program that finds the greatest of given 5 variables.
using System;

class GreatestOfFiveVariables
{
    static void Main()
    {
        Console.WriteLine("Enter the first variable: ");
        int firstVariable = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second variable: ");
        int secondVariable = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the third variable: ");
        int thirdVariable = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the fourth variable: ");
        int fourthVariable = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the fifth variable: ");
        int fifthVariable = int.Parse(Console.ReadLine());
        if (firstVariable > secondVariable & firstVariable > thirdVariable & firstVariable > fourthVariable & firstVariable > fifthVariable)
        {
            Console.WriteLine("The biggest number is: {0}", firstVariable);
        }
        else if (secondVariable > firstVariable & secondVariable > thirdVariable & secondVariable > fourthVariable & secondVariable > fifthVariable)
        {
            Console.WriteLine("The biggest number is: {0}", secondVariable);
        }
        else if (thirdVariable > firstVariable & thirdVariable > secondVariable & thirdVariable > fourthVariable & thirdVariable > fifthVariable)
        {
            Console.WriteLine("The biggest number is: {0}", thirdVariable);
        }
        else if (fourthVariable > firstVariable & fourthVariable > secondVariable & fourthVariable > thirdVariable & fourthVariable > fifthVariable)
        {
            Console.WriteLine("The biggest number is: {0}", fourthVariable);
        }
        else if (fifthVariable > firstVariable & fifthVariable > secondVariable & fifthVariable > thirdVariable & fifthVariable > fourthVariable)
        {
            Console.WriteLine("The biggest number is: {0}", fifthVariable);
        }
    }
}

8. 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;

class RecognizeTypeOfVariable
{
    static void Main()
    {
        Console.WriteLine("Choose type of variable: int , double or string:");
        string variable = Console.ReadLine();
        Console.WriteLine("Enter a value for the variable");
        string value = Console.ReadLine();
        switch (variable)
        {
            case "int": Console.WriteLine(int.Parse(value) + 1); break;
            case "double": Console.WriteLine(double.Parse(value) + 1); break;
            case "string": Console.WriteLine(value + "*"); break;
            default: Console.WriteLine("Error"); break;
        }
    }
}

9. 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;

class SumOfSubset
{
    static void Main()
    {
        Console.WriteLine("Enter the first number:");
        int firstNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the second number: ");
        int secondNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the third number:");
        int thirdNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the fourth number:");
        int fourthNumber = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter the fifth number: ");
        int fifthNumber = int.Parse(Console.ReadLine());
        if (firstNumber + secondNumber + thirdNumber == 0 | firstNumber + secondNumber + fourthNumber == 0 | firstNumber + secondNumber + fifthNumber == 0)
        {
            Console.WriteLine("The sum of some subset is equal to zero");
        }
        else if (firstNumber + thirdNumber + fourthNumber == 0 | firstNumber + thirdNumber + fifthNumber == 0 | firstNumber + fourthNumber + fifthNumber == 0)
        {
            Console.WriteLine("The sum of some subset is equal to zero");
        }
        else if (secondNumber + thirdNumber + fourthNumber == 0 | secondNumber + thirdNumber + fifthNumber == 0 | secondNumber + fourthNumber + fifthNumber == 0 | thirdNumber + fourthNumber + fifthNumber == 0)
        {
            Console.WriteLine("The sum of some subset is equal to zero");
        }
        else
        {
            Console.WriteLine("There is no sum of subset equal to zero.");
        }
    }
}

10. 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;

class BonusScores
{
    static void Main()
    {
        Console.WriteLine("Enter one digit:");
        string digit = Console.ReadLine();
        int newDigit;
        bool check = int.TryParse(digit, out newDigit);
        if (!check)
        {
            Console.WriteLine("Not a digit");
        }
        else
        {
            switch (newDigit)
            {
                case 1: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 10); break;
                case 2: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 10); break;
                case 3: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 10); break;
                case 4: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 100); break;
                case 5: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 100); break;
                case 6: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 100); break;
                case 7: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 1000); break;
                case 8: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 1000); break;
                case 9: Console.WriteLine("Result is : {0}" , int.Parse(digit) * 1000); break;
                case 0: Console.WriteLine("Error"); break;
                default:
                    break;
            }
        }
    }
}

10. 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;

class NumbersToText
{
    static void Main()
    {
        Console.WriteLine("Enter one number between 0 and 999");
        int number = int.Parse(Console.ReadLine());
        if (number > 99 & number <= 999)
        {
            int hundreds = number / 100;

            switch (hundreds)
            {
                case 1: Console.Write("One hundred "); break;
                case 2: Console.Write("Two hundred "); break;
                case 3: Console.Write("Three hundred "); break;
                case 4: Console.Write("Four hundred "); break;
                case 5: Console.Write("Five hundred "); break;
                case 6: Console.Write("Six hundred "); break;
                case 7: Console.Write("Seven hundred "); break;
                case 8: Console.Write("Eight hundred "); break;
                case 9: Console.Write("Nine hundred "); break;
                default:
                    break;
            }
            int tens = ((number) - (hundreds * 100));

            if (tens >= 20)
            {
                tens = tens / 10;
                switch (tens)
                {
                    case 2: Console.Write("and twenty"); break;
                    case 3: Console.Write("and thirty"); break;
                    case 4: Console.Write("and fourty"); break;
                    case 5: Console.Write("and fifty"); break;
                    case 6: Console.Write("and sixty"); break;
                    case 7: Console.Write("and seventy"); break;
                    case 8: Console.Write("and eighty"); break;
                    case 9: Console.Write("and ninety"); break;
                    default:
                        break;
                }
                int ones = ((number) - (hundreds * 100 + tens * 10));

                switch (ones)
                {
                    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:
                        break;
                }
            }
            else
            {

                switch (tens)
                {
                    case 1: Console.WriteLine("and one"); break;
                    case 2: Console.WriteLine("and two"); break;
                    case 3: Console.WriteLine("and three"); break;
                    case 4: Console.WriteLine("and four"); break;
                    case 5: Console.WriteLine("and five"); break;
                    case 6: Console.WriteLine("and six"); break;
                    case 7: Console.WriteLine("and seven"); break;
                    case 8: Console.WriteLine("and eight"); break;
                    case 9: Console.WriteLine("and nine"); break;
                    case 10: Console.WriteLine("and ten"); break;
                    case 11: Console.WriteLine("and eleven"); break;
                    case 12: Console.WriteLine("and twelve"); break;
                    case 13: Console.WriteLine("and thirteen"); break;
                    case 14: Console.WriteLine("and fourteen"); break;
                    case 15: Console.WriteLine("and fifteen"); break;
                    case 16: Console.WriteLine("and sixteen"); break;
                    case 17: Console.WriteLine("and seventeen"); break;
                    case 18: Console.WriteLine("and eighteen"); break;
                    case 19: Console.WriteLine("and nineteen"); break;

                    default:
                        break;
                }
            }
        }
        if (number < 100 & number > 19)
        {
            int tensSecond = number / 10;
            switch (tensSecond)
            {
                case 2: Console.Write("Twenty"); break;
                case 3: Console.Write("Thirty"); break;
                case 4: Console.Write("Fourty"); break;
                case 5: Console.Write("Fifty"); break;
                case 6: Console.Write("Sixty"); break;
                case 7: Console.Write("Seventy"); break;
                case 8: Console.Write("Eighty"); break;
                case 9: Console.Write("Ninety"); break;
                default:
                    break;
            }
            int onesSecond = number - tensSecond * 10;
            switch (onesSecond)
            {
                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:
                    break;
            }
        }
        else
        {
            switch (number)
            {
                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;
                case 10: Console.WriteLine("Ten"); break;
                case 11: Console.WriteLine("Eleven"); break;
                case 12: Console.WriteLine("Twelve"); break;
                case 13: Console.WriteLine("Thirteen"); break;
                case 14: Console.WriteLine("Fourteen"); break;
                case 15: Console.WriteLine("Fifteen"); break;
                case 16: Console.WriteLine("Sixteen"); break;
                case 17: Console.WriteLine("Seventeen"); break;
                case 18: Console.WriteLine("Eighteen"); break;
                case 19: Console.WriteLine("Nineteen"); break;

                default:
                    break;
            }
        }
    }
}

Няма коментари:

Публикуване на коментар