Function Writerectangle - C# Programming Exercise

In this exercise of C#, you need to create two functions: one called WriteRectangle to display a filled rectangle on the screen using asterisks, and another called WriteHollowRectangle to display only the border of the rectangle. Both functions will take width and height as parameters. In the first function, WriteRectangle, when called with the values (4, 3), it should display a filled rectangle like this:
****
****
****
In the second function, WriteHollowRectangle, when called with the values (3, 4), it should display only the border of the rectangle:
***
* *
* *
***
This exercise in C# will help you practice loops, string manipulation, and using parameters in functions. Additionally, it will improve your skills in displaying data in the console, which is an essential skill for developing applications in C#.

You will learn how to structure functions to perform common tasks efficiently and how to generate visual patterns using characters, which is an important concept in many programming projects.

 Category

Functions

 Exercise

Function Writerectangle

 Objective

Write a C# function WriteRectangle to display a (filled) rectangle on the screen, with the width and height indicated as parameters, using asterisks. Complete the test program with a Main function:

WriteRectangle(4,3);

should display
****
****
****

Create also a function WriteHollowRectangle to display only the border of the rectangle:
WriteHollowRectangle(3,4);

should display
***
* *
* *
***

 Write Your C# Exercise

// Import the System namespace to use basic classes like Console
using System;

class Program
{
    // Function to display a filled rectangle
    public static void WriteRectangle(int width, int height)
    {
        // Loop through each row
        for (int i = 0; i < height; i++)
        {
            // Loop through each column and print an asterisk
            for (int j = 0; j < width; j++)
            {
                Console.Write("*");  // Print an asterisk
            }
            // Move to the next line after each row
            Console.WriteLine();
        }
    }

    // Function to display a hollow rectangle (border only)
    public static void WriteHollowRectangle(int width, int height)
    {
        // Loop through each row
        for (int i = 0; i < height; i++)
        {
            // Loop through each column in the row
            for (int j = 0; j < width; j++)
            {
                // Check if we are at the border of the rectangle (first/last row or column)
                if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
                {
                    Console.Write("*");  // Print an asterisk at the border
                }
                else
                {
                    Console.Write(" ");  // Print a space for the hollow part
                }
            }
            // Move to the next line after each row
            Console.WriteLine();
        }
    }

    // Main function to test the WriteRectangle and WriteHollowRectangle functions
    public static void Main()
    {
        // Display a filled rectangle with width 4 and height 3
        Console.WriteLine("Filled Rectangle:");
        WriteRectangle(4, 3);
        
        // Add a line break for better readability
        Console.WriteLine();

        // Display a hollow rectangle with width 3 and height 4
        Console.WriteLine("Hollow Rectangle:");
        WriteHollowRectangle(3, 4);
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

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#.

  •  Function Palindrome, iterative

    In this exercise in C#, you need to create an iterative function that checks if a string is symmetric (a palindrome). A palindrome is a word, number, phrase, or other...

  •  Function Palindrome, recursive

    In this exercise in C#, you will need to create a recursive function to check if a string is symmetric (a palindrome). A palindrome is a word, number, phrase, ...

  •  Function GetMinMax

    In this exercise in C#, you will need to write a function named "GetMinMax", which will ask the user to enter a minimum value (a number) and a maximum value (a...

  •  Function Multiply & MultiplyR

    In this exercise in C#, you will need to write two functions called "Multiply" and "MultiplyR" to calculate the product of two numbers using sums. The first ve...

  •  Functions: greeting + farewell

    In this C# exercise, you are asked to write a program where the main structure is the Main method. Inside this method, two functions should be called: SayHello...

  •  Function with parameters

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ SayHello("John"...