Formats - C# Programming Exercise

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.

 Category

First contact with C# Sharp

 Exercise

Formats

 Objective

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

 Write Your C# Exercise

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
    }
}

 Share this C# exercise

 More C# Programming Exercises of First contact with C# Sharp

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Rectangle

    This C# exercise helps you practice using loops and user input. In this program, the user will be asked to input a number (a digit), and the program will display a re...

  •  Conversion

    This C# exercise teaches you how to perform temperature unit conversions. The program prompts the user to input a temperature in Celsius degrees and then converts it ...

  •  First contact with C#

    This exercise is an excellent way to get started with programming in C#. In this program, you will learn how to print a message on the screen using C#. The program wi...

  •  Sum of two numbers

    This exercise is a great way to practice basic arithmetic operations in C#. In this program, you will learn how to perform an addition operation between two numbers, 12 and ...

  •  Division of two numbers

    This exercise is a great opportunity to practice the division operation in C#. In this program, you will learn how to divide two numbers, in this case, 24 and 5, and ...

  •  Multiple operations and precedences

    This exercise is perfect for learning how to perform complex arithmetic operations in C#. In this program, you will be asked to solve several mathematical operations using d...