C# For Dummies

Chapter 5. Conditional Statements

Write an if- statement that takes two integer variables and exchanges their values if the first one is greater than the second one.

Guidelines: Read about if-statements.

Solution
static void Main(string[] args)
{    
    Console.Write("Enter first number: ");
    int a = Int32.Parse(Console.ReadLine());
    Console.Write("Enter second number: ");
    int b = Int32.Parse(Console.ReadLine());    

    if (a > b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }
    
    Console.WriteLine("First number - {0}, Second number - {1}.", a, b);
}

2. Write a program that shows the sign (+ or -) of the product of three real numbers, without calculating it. Use a sequence of if operators.

Guidelines: A multiple of non-zero numbers has a positive product, if the negative multiples are even number. If the count of the negative numbers is odd, the product is negative. If at least one of the numbers is zero, the product is also zero. Use a counter negativeNumbersCount to keep the number of negative numbers. Check each number whether it is negative and change the counter accordingly. If some of the numbers is 0, print “0” as result (the zero has no sign). Otherwise print “+” or “-” depending on the condition (negativeNumbersCount % 2 == 0) .

Solution
Link
3. Write a program that finds the biggest of three integers, using nested if statements.

Guidelines: Use nested if-statements, first checking the first two numbers then checking the bigger of them with the third.

Solution
Link
4. Sort 3 real numbers in descending order. Use nested if statements.

Guidelines: First find the smallest of the three numbers, and then swap it with the first one. Then check if the second is greater than the third number and if yes, swap them too.
Another approach is to check all possible orders of the numbers with a series of if-else checks: a≤b≤c, a≤c≤b, b≤a≤c, b≤c≤a, c≤a≤b and c≤b≤a.
A more complicated and more general solution of this problem is to put the numbers in an array and use the Array.Sort(…) method.

Solution
Link
5. Write a program that asks for a digit (0-9), and depending on the input, shows the digit as a word in a Bulgarian or English. Use a switch statement.

Guidelines: Just use a switch statement to check for all possible digits.

Solution
static void Main(string[] args)
{    
    Console.Write("Enter first number: ");
    int number = Int32.Parse(Console.ReadLine());

    switch (number)
    {
        case 0: Console.WriteLine("Nula"); break;
        case 1: Console.WriteLine("Edno"); break;
        case 2: Console.WriteLine("Dve"); break;
        case 3: Console.WriteLine("Tri"); break;
        case 4: Console.WriteLine("Chetiri"); break;
        case 5: Console.WriteLine("Pet"); break;
        case 6: Console.WriteLine("Shest"); break;
        case 7: Console.WriteLine("Sedem"); break;
        case 8: Console.WriteLine("Osem"); break;
        case 9: Console.WriteLine("Devet"); break;
        default: Console.WriteLine("Wrong input"); break;
    }
}

6. Write a program that gets the coefficients a, b and c of a quadratic equation: ax2 + bx + c, calculates and prints its real roots (if they exist). Quadratic equations may have 0, 1 or 2 real roots.

Guidelines: Look for the formulas for solving a quadratic equation.

Solution
Link
7. Write a program that finds the greatest of given 5 numbers.

Guidelines: Use nested if statements. You could use the loop structure for, which you could read about on the Internet.

Solution
static void Main(string[] args)
{    
    Console.Write("Enter first number: ");
    int a = Int32.Parse(Console.ReadLine());
    Console.Write("Enter second number: ");
    int b = Int32.Parse(Console.ReadLine());
    Console.Write("Enter third number: ");
    int c = Int32.Parse(Console.ReadLine());
    Console.Write("Enter fourth number: ");
    int d = Int32.Parse(Console.ReadLine());
    Console.Write("Enter fifth number: ");
    int e = Int32.Parse(Console.ReadLine());
    
    if (a < b) a = b;
    if (a < c) a = c;
    if (a < d) a = d;
    if (a < e) a = e;
    
    Console.WriteLine("{0} is the biggest number.", a);
}

8. Write a program that, depending on the user’s choice, inputs int, double or string variable. If the variable is int or double, the program increases it by 1. If the variable is a string, the program appends \"*\" at the end. Print the result at the console. Use switch statement.

Guidelines: First input a variable, which indicates what type will be the input, i.e. by entering 0 the type is int, by 1 is double and by 2 is string.

Solution
Link
9. We are given 5 integer numbers. Write a program that finds those subsets whose sum is 0. Examples:
- If we are given the numbers {3, -2, 1, 1, 8}, the sum of -2, 1 and 1 is 0.
>- If we are given the numbers {3, 1, -7, 35, 22}, there are no subsets with sum 0.

Guidelines: Use nested if statements or series of 31 comparisons, in order to check all the sums of the 31 subsets of the given numbers (without the empty one). Note that the problem in general (with N numbers) is complex and using loops will not be enough to solve it.

Solution
Link
10. Write a program that applies bonus points to given scores in the range [1…9] by the following rules:
- If the score is between 1 and 3, the program multiplies it by 10.
- If the score is between 7 and 9, the program multiplies it by 1000.
- If the score is 0 or more than 9, the program prints an error message.
Guidelines: Use switch statement or a sequence of if-else constructs and at the end print at the console the calculated points.

Solution
Link
Write a program that converts a number in the range [0…999] to words, corresponding to the English pronunciation. Examples:
- 0 → "Zero"
- 12 → "Twelve"
- 98 → "Ninety eight"
- 273 → "Two hundered seventy three"
- 400 → "Four hundred"
- 501 → "Five hundred and one"
- 711 → "Seven hundered and eleven"

Guidelines: Use nested switch statements. Pay special attention to the numbers from 0 to 19 and those that end with 0. There because printing a single digit is part of printing a 2-digit number which is part of printing 3-digit number.

Solution
Link

← Back