Functions: Greeting + Farewell - C# Programming Exercise

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 and SayGoodbye. The task is to define both functions, which will handle displaying a greeting message and a farewell message respectively. The exercise aims to familiarize you with creating methods in C# and calling them from the main method, helping you understand the basic flow of execution in a function-oriented program.

 Category

Functions

 Exercise

Functions: Greeting + Farewell

 Objective

Write a C# program whose Main must be like this:

public static void Main()
{
SayHello();
SayGoodbye();
}

SayHello and SayGoodbye are functions that you must define and that will be called from inside Main.

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to print a greeting message
    public static void SayHello()
    {
        // Output a greeting message
        Console.WriteLine("Hello, welcome to the program!");
    }

    // Function to print a farewell message
    public static void SayGoodbye()
    {
        // Output a farewell message
        Console.WriteLine("Goodbye, thanks for using the program!");
    }

    // Main method to call SayHello and SayGoodbye functions
    public static void Main()
    {
        // Call the SayHello function to greet the user
        SayHello();

        // Call the SayGoodbye function to bid farewell to the user
        SayGoodbye();
    }
}

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

  •  Function returning a value

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ int x = 3;

  •  Function returning a value V2

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

  •  Function write centered

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters). The code should...

  •  Function write underlined

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters) and then underli...

  •  Function sum of array

    In this C# exercise, you are asked to write a program to calculate the sum of the elements in an array. The Main function should look like the following:<...