C# Exercise: 12 Formats

This C# exercise teaches you how to handle console output using two different methods: Console.Write and formatting with {0}. In this program, the user will input a number, and the program will display the number four times in a row, separated by spaces, and then four times in the next row with no separation.

The goal is to practice how to use Console.Write to print on the same line without moving to a new line, and also how to format the output using the {0} placeholder in C# for more flexible value printing.

This exercise is essential for understanding how to manipulate console output in C#, allowing you to control the arrangement of the data displayed on the screen, a useful concept when creating interactive programs.

 Exercise

Formats

 Objetive

Write a C# program to ask the user for a number and display it four times in a row, separated with blank spaces, and then four times in the next row, with no separation. You must do it two times: first using Console.Write and then using {0}.

Example:
Enter a number: 3
3 3 3 3
3333
3 3 3 3
3333

 Example Code

using System; // Importing the System namespace to use Console functionalities

// Main class of the program
class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Declaring a variable to store the number entered by the user
        int number;

        // Asking the user to enter a number and reading the input
        Console.Write("Enter a number: ");
        number = Convert.ToInt32(Console.ReadLine());

        // Using Console.Write to display the number four times in a row, separated by blank spaces
        Console.WriteLine(); // Adding a newline before the first output
        Console.Write(number + " " + number + " " + number + " " + number); // Printing the number with spaces
        Console.WriteLine(); // Adding a newline after the first output

        // Using Console.Write to display the number four times in the next row, with no separation
        Console.Write(number.ToString() + number.ToString() + number.ToString() + number.ToString()); // Printing the number without spaces
        Console.WriteLine(); // Adding a newline after the second output

        // Using {0} formatting to display the number four times in a row, separated by blank spaces
        Console.WriteLine("{0} {0} {0} {0}", number); // Printing the number with spaces

        // Using {0} formatting to display the number four times in the next row, with no separation
        Console.WriteLine("{0}{0}{0}{0}", number); // Printing the number without spaces
    }
}

More C# Exercises of First contact with C# Sharp

 First contact with C#
Write a C# program to print Hello on screen and then print your name (in a separate line)....
 Sum of two numbers
Write a C# program to print the result of adding 12 and 13 on screen....
 Division of two numbers
Write a C# program to print the result of dividing 24 by 5 on the screen....
 Multiple operations and precedences
Write a C# program to print the result of the following operations: -1 + 3 * 5 (24 + 5) % 7 15 + (-4) * 6 / 11 2 + 10 / 6 * 1 - 7 % 2...
 Multiply using variables
Write a C# program to print the result of multiplying two numbers which will entered by the user....
 Use of {0} and comments
Write a C# program to ask the user for three numbers and display their multiplication. The first line must be a comment with your name and surname. It...
 Several operations
Write a C# program to print on screen the result of adding, subtracting, multiplying and dividing two numbers typed by the user. The remainder of the ...
 Multiplication table
Write a C# program to ask the user for a number and display its multiplication table, like this: Please enter a number: 5 The multiplication ta...
 Average
Write a C# program to calculate and display the average of four numbers entered by the user....
 Equivalent operations
Write a C# program to ask the user for three numbers (a, b, c) and display the result of (a+b)·c and the result of a·c + b·c....
 Age
Write a C# program to ask the user for their age (e.g. 20) and respond with something like "You look younger than 20" (the age entered by the user sho...
 Rectangle
Write a C# program to ask the user for a number and then display a rectangle 3 columns wide and 5 rows tall using that digit. For example: Enter a ...
 Conversion
Write a C# program to convert Celsius degrees to Kelvin and Fahrenheit. The program will prompt the user to input the temperature in Celsius degrees, ...