C# For Dummies

Chapter 4. Console Input and Output


1. Write a program that reads from the console three numbers of type int and prints their sum.

Guidelines: Use the methods Console.ReadLine() and Int32.Parse().

Solution:
static void Main(string[] args)
{
    Console.Write("Enter radius: ");
    int a = Int32.Parse(Consle.ReadLine());
    Console.Write("Second number: ");
    int b = Int32.Parse(Consle.ReadLine());
    Console.Write("Third number: ");
    int c = Int32.Parse(Consle.ReadLine());
    Console.WriteLine("Result is {0}", a + b + c);
}

2. Write a program that reads from the console the radius "r" of a circle and prints its perimeter and area.

Guidelines: Use Math.PI constant and the well-known geometric formulas.

Solution:
static void Main(string[] args)
{
    Console.Write("Enter radius: ");
    int r = Int32.Parse(Consle.ReadLine());
    Console.WriteLine("P is {0} and S is {1}", 2 * Math.PI * r, Math.PI * r * r);
}

3. A given company has name, address, phone number, fax number, web site and manager. The manager has name, surname and phone number. Write a program that reads information about the company and its manager and then prints it on the console.

Guidelines: Format the text with Write(…) or WriteLine(…).

Solution:
Link
4. Write a program that prints three numbers in three virtual columns on the console. Each column should have a width of 10 characters and the numbers should be left aligned. The first number should be an integer in hexadecimal; the second should be fractional positive; and the third – a negative fraction. The last two numbers have to be rounded to the second decimal place.

Guidelines: Use format strings and the method Console.WriteLine().

Solution:
static void Main(string[] args)
{
    int hexNum = 2015;
    Console.WriteLine("|0x{0,-8:X|", hexNum);
    double fractNum = -1.856;
    Console.WriteLine("|0,-10:f2}|", fractNum);
}

5. Write a program that reads from the console two integer numbers (int) and prints how many numbers between them exist, such that the remainder of their division by 5 is 0. Example: in the range (14, 25) there are 3 such numbers: 15, 20 and 25.

Guidelines: There are two approaches for solving the problem:
First approach: Use mathematical tricks for optimized calculation based on the fact that every fifth number is divisible by 5. Think how to implement this correctly and about the borderline cases.
The second approach is easier but it works slower. With a for-loop each number within the given range can be checked. You should read on the Internet about how to use for-loops.

Solution:
static void Main(string[] args)
{
    int counter = 0;
    
    Console.Write("Enter first number: ");
    int a = Int32.Parse(Console.ReadLine());
    Console.Write("Enter second number: ");
    int b = Int32.Parse(Console.ReadLine());
    
    for (int i = a; i <= b; i++)
    {
        if (i % 5 == 0) counter++;
    }
    
    Console.WriteLine("{0} numbers found.", counter);
}

6. Write a program that reads two numbers from the console and prints the greater of them. Solve the problem without using conditional statements.

Guidelines: Since the problem requires a solution, which does not use conditional statements, you should use a different approach. Two possible solutions of the problem include the use of functions of class Math. The greater of the two numbers you can find with the function Math.Max(a, b) and the smaller with Math.Min(a, b).

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.WriteLine("{0} >= {1}", Math.Max(a, b), Math.Min(a, b));
}

7. Write a program that reads five integer numbers and prints their sum. If an invalid number is entered the program should prompt the user to enter another number.

Guidelines: You can read the numbers in five different variables and finally sum them and print the obtained sum. Note that the sum of 5 int values may not fit in the int type so you should use long.

Solution:
Link
8. Write a program that reads five numbers from the console and prints the greatest of them.

Guidelines: You can use the comparison statement \"if\". You can read about it on the Internet.

Solution:
Link
9. Write a program that reads a, b and c from the console and calculates: ax2+bx+c=0.

Guidelines: Use the method for calculating quadratic equations.

Solution:
Link
10. Write a program that reads an integer number n from the console. After that reads n numbers from the console and prints their sum.

Guidelines: You should use a for-loop. Read the numbers one after another and accumulate their sum in a variable, which then display on the console at the end.

Solution:
static void Main(string[] args)
{    
    int sum = 0;
    
    Console.Write("Enter numbers count: ");
    int length = Int32.Parse(Console.ReadLine());
    
    for (int i = 0; i < length; i++)
    {
        Console.Write("Enter {0} number: ", i + 1);
        sum += Int32.Parse(Console.ReadLine());
    }
    
    Console.WriteLine("Sum of all numbers is {0}.", sum);
}

11. Write a program that reads an integer number n from the console and prints all numbers in the range [1…n], each on a separate line.

Guidelines: Use a combination of loops and the methods Console.ReadLine(), Console.WriteLine() and Int32.Parse().

Solution:
static void Main(string[] args)
{    
    int sum = 0;
    
    Console.Write("Enter number: ");
    int length = Int32.Parse(Console.ReadLine());
    
    for (int i = 1; i <= length; i++)
    {
        Console.WriteLine(i);
    }    
}

12. Write a program that prints on the console the first 100 numbers in the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …

Guidelines: For the solution of the problem use 2 temporary variables in which store the last 2 calculated values and with a loop calculate the rest (each subsequent number in the sequence is a sum of the last two). Use a for-loop to implement the repeating logic.

Solution:
static void Main(string[] args)
{    
    int num1 = 0;
    int num2 = 1;
    int sum = 1;
    int count = 0;
    
    Console.WriteLine(num1);
    
    while(count < 100)
    {
        sum = num1 + num2;
        num1 = num2;
        num2 = sum;
        Console.WriteLine(num2);
        count++;
    }
}

13. Write a program that calculates the sum (with precision of 0.001) of the following sequence: 1 + 1/2 - 1/3 + 1/4 - 1/5 + …

Guidelines: Accumulate the sum of the sequence in a variable inside a while-loop. At each step compare the old sum with the new sum. If the difference between the two sums Math.Abs(current_sum – old_sum) is less than the required precision (0.001), the calculation should finish because the difference is constantly decreasing and the precision is constantly increasing at each step of the loop. The expected result is 1.307.

Solution:
static void Main(string[] args)
{    
    Console.Write("Enter last number: ");
    int length = Int32.Parse(Console.ReadLine());
    double sum = 1.0;
    
    for (int i = 2; i <= length; i++)
    {
        sum += (1.0 / i);
    }
    
    Console.WriteLine("{0:F3}", sum);
}

← Back