Function write centered Learn programming C#

Lesson:

Functions


Exercise:

Function write centered


Objetive:

Create a function to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 characters):

WriteCentered("Hello!");


Code:

using System;
public class exercise101
{
    public static void WriteCentered(string text)
    {
        int i = 0;

        for (; i < 17; i++)
            Console.WriteLine();

        for (i = 0; i < 36; i++)
            Console.Write(" ");

        Console.Write(text);

        for (i = 0; i < 14; i++)
            Console.WriteLine();
    }

    public static void Main()
    {
        WriteCentered("Hello");
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.