How to resolve the algorithm Terminal control/Coloured text step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Terminal control/Coloured text step by step in the C# programming language

Table of Contents

Problem Statement

Display a word in various colours on the terminal. The system palette, or colours such as Red, Green, Blue, Magenta, Cyan, and Yellow can be used.

Optionally demonstrate:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Terminal control/Coloured text step by step in the C# programming language

This code demonstrates the use of the Console class to change the foreground and background colors of the console window and to reset them back to the default values.

The Main() method is the entry point of the program.

The first line of code sets the foreground color of the console window to red and the background color to yellow.

Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Yellow;

The next line of code writes the string "Red on Yellow" to the console window.

Console.WriteLine("Red on Yellow");

The next two lines of code set the foreground color of the console window to white and the background color to black.

Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;

The next line of code writes the string "White on black" to the console window.

Console.WriteLine("White on black");

The next line of code resets the foreground and background colors of the console window to their default values.

Console.ResetColor();

The last line of code writes the string "Back to normal" to the console window.

Console.WriteLine("Back to normal");

The ReadKey() method is called to wait for the user to press a key before closing the console window.

Console.ReadKey();

Source code in the csharp programming language

static void Main(string[] args)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.BackgroundColor = ConsoleColor.Yellow;
    Console.WriteLine("Red on Yellow");
    Console.ForegroundColor = ConsoleColor.White;
    Console.BackgroundColor = ConsoleColor.Black;
    Console.WriteLine("White on black");
    Console.ResetColor();
    Console.WriteLine("Back to normal");
    Console.ReadKey();
}


  

You may also check:How to resolve the algorithm Mutual recursion step by step in the Elixir programming language
You may also check:How to resolve the algorithm 24 game step by step in the Haskell programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Factor programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Julia programming language