Function Parameters Of Main, Sum - C# Programming Exercise

In this exercise of C#, you will learn how to create a program named sum that receives two integer numbers from the command line and displays their sum. This type of exercise is essential for understanding how to work with command line inputs in C# and how to perform basic mathematical operations, such as addition, with values provided by the user. In the example, if the program is executed with the parameters "sum 5 3", the program will calculate the sum of the two numbers and display the result, which is 8. This exercise is perfect for getting familiar with using command line parameters and improving your skills in C# to develop interactive and practical applications.

Learn how to interact with the user through the command line in C# and how to perform simple operations like adding numbers with this hands-on exercise.

 Category

Functions

 Exercise

Function Parameters Of Main, Sum

 Objective

Write a C# program named "sum", which receives two integer numbers in the command line and displays their sum, as in this example:

sum 5 3
8

 Example C# Exercise

 Copy C# Code
// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Main method where the program starts
    public static void Main(string[] args)
    {
        // Checking if the user has provided two arguments
        if (args.Length == 2)
        {
            try
            {
                // Parsing the arguments to integers safely
                int num1 = int.Parse(args[0]); // The first number provided in the command line
                int num2 = int.Parse(args[1]); // The second number provided in the command line

                // Calculating and displaying the sum of the two numbers
                Console.WriteLine(num1 + num2); // The sum of num1 and num2 is printed
            }
            catch (FormatException) 
            {
                // If the user inputs non-numeric values, catch the exception and display an error message
                Console.WriteLine("Error: Please enter valid integers.");
            }
        }
        else
        {
            // If the user does not provide exactly two arguments, display an error message
            Console.WriteLine("Please provide exactly two numbers.");
        }
    }
}

 Output

-- Case 1: Execution with two valid numbers
dotnet run 5 7
12

-- Case 2: Non-numeric input (errors when trying to convert arguments to integers)
dotnet run 5 a
Error: Please enter valid integers.

-- Case 3: Fewer than two arguments
dotnet run 5
Please provide exactly two numbers.

-- Case 4: More than two arguments
dotnet run 5 7 10
Please provide exactly two numbers.

 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 SumDigits

    In this exercise of C#, you will learn how to create a function named SumDigits that receives a number and returns the result of summing its digits. Thi...

  •  Function Factorial

    In this exercise of C#, you will learn how to create a recursive function to calculate the factorial of a number. The factorial of a number is ex...

  •  Function Parameters of Main, Reverse

    In this exercise of C#, you will learn how to create a program named reverse that receives several words from the command line and display...

  •  Function GetInt

    In this exercise of C#, you will learn how to create a function named GetInt, which displays on screen the text received as a parameter, asks the user f...

  •  Function tasks database

    In this exercise of C#, you will improve a "tasks database" program by splitting it into multiple functions. This type of exercise is great for l...

  •  Function Greatest value in a array

    In this exercise of C#, you will learn how to create a function that returns the greatest value stored in an array of real numbers, which is specified as a par...