Function Calculator, Params Of Main - C# Programming Exercise

In this exercise of C#, you will learn how to create a program that performs mathematical operations such as addition, subtraction, multiplication, or division, by analyzing the command line parameters. The program should receive three parameters: a number, an operation sign, and another number, like this example: calc 5 + 379. The allowed signs are +, -, *, x, and /. The program should identify the entered sign and perform the corresponding operation. This exercise is ideal for practicing command-line input analysis, handling arithmetic operations in C#, and performing parameter validation to ensure the operation is correct. Additionally, you will learn how to handle different mathematical operations and how to process them efficiently in your program.

This exercise is a great way to understand how to receive parameters from the command line and perform dynamic operations based on those parameters, enhancing your skills in working with C# and interacting with the user via the terminal or console.

 Category

Functions

 Exercise

Function Calculator, Params Of Main

 Objective

Write a C# program to calculate a sum, subtraction, product or division, analyzing the command line parameters:

calc 5 + 379

(Parameters must be a number, a sign, and another number; allowed signs are + - * x / )

 Write Your C# Exercise

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

class Program
{
    // Main method to drive the program
    public static void Main(string[] args)
    {
        // Check if the correct number of arguments (3) are passed
        if (args.Length != 3)
        {
            Console.WriteLine("Error: Please provide two numbers and an operator.");
            return;
        }

        // Parse the first number and operator from the command line arguments
        double num1 = double.Parse(args[0]);
        string operatorSign = args[1];
        double num2 = double.Parse(args[2]);

        // Perform the calculation based on the operator
        double result = 0;
        bool validOperation = true;

        // Switch statement to handle different operations
        switch (operatorSign)
        {
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            case "*":
            case "x": // Allow 'x' as multiplication sign
                result = num1 * num2;
                break;
            case "/":
                if (num2 == 0)
                {
                    Console.WriteLine("Error: Cannot divide by zero.");
                    validOperation = false;
                }
                else
                {
                    result = num1 / num2;
                }
                break;
            default:
                Console.WriteLine("Error: Invalid operator. Use +, -, *, x, or /.");
                validOperation = false;
                break;
        }

        // Output the result if the operation is valid
        if (validOperation)
        {
            Console.WriteLine($"Result: {result}");
        }
    }
}

 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 calculator, params and return value of Main

    In this exercise of C#, you will create a program that calculates mathematical operations like addition, subtraction, multiplication, or division, by analyzing...

  •  Function MinMaxArray

    In this exercise of C#, you will create a function named MinMaxArray that receives an array of numbers and returns the minimum and maximum values using ...

  •  Function Reverse, recursive

    In this exercise of C#, you need to create a program that uses recursion to reverse a string of characters. The program should receive a string as input...

  •  Function WriteRectangle

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

  •  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, ...