Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Task - Building a Simple Calculator | Introduction
Introduction to .NET with C#

Task - Building a Simple CalculatorTask - Building a Simple Calculator

How to solve tasks:

Task:

Create a basic calculator program. It should work like this:

  1. Ask the user for a math operation and store it in a char variable called opr;
  2. Ask the user for the first number, and store it in a float variable called first;
  3. Ask the user for the second number, and store it in a float variable called second;
  4. Apply the operator on the two numbers and display the result.

The entered operation will be of type string, and the user will enter one of the four options: +, -, /, *.

You don't need to deal with cases where the user enters an invalid operation or an invalid number.

The resulting output must be in the following format otherwise, the test cases might fail:

Here first, second and result are float values.

Link to the Task: GitHub
Use conditional if-else statements or the switch statement to check which operation the user has entered, and apply the operation on the two numbers based on that.

public class Program
{
    public static void Main(string[] args)
    {
        Console.Write("Enter an operation (+, -, /, *): ");
        char opr = Console.ReadLine()[0];

        Console.Write("Enter the first number: ");
        float first = float.Parse(Console.ReadLine());

        Console.Write("Enter the second number: ");
        float second = float.Parse(Console.ReadLine());

        float result = 0;
        if (opr == '+')
            result = first + second;
        else if (opr == '-')
            result = first - second;
        else if (opr == '/')
            result = first / second;
        else if (opr == '*')
            result = first * second;

        Console.WriteLine($"The result of '{first} {opr} {second}' is: {result}");
    }
}    
      

Все було зрозуміло?

Секція 1. Розділ 4
course content

Зміст курсу

Introduction to .NET with C#

Task - Building a Simple CalculatorTask - Building a Simple Calculator

How to solve tasks:

Task:

Create a basic calculator program. It should work like this:

  1. Ask the user for a math operation and store it in a char variable called opr;
  2. Ask the user for the first number, and store it in a float variable called first;
  3. Ask the user for the second number, and store it in a float variable called second;
  4. Apply the operator on the two numbers and display the result.

The entered operation will be of type string, and the user will enter one of the four options: +, -, /, *.

You don't need to deal with cases where the user enters an invalid operation or an invalid number.

The resulting output must be in the following format otherwise, the test cases might fail:

Here first, second and result are float values.

Link to the Task: GitHub
Use conditional if-else statements or the switch statement to check which operation the user has entered, and apply the operation on the two numbers based on that.

public class Program
{
    public static void Main(string[] args)
    {
        Console.Write("Enter an operation (+, -, /, *): ");
        char opr = Console.ReadLine()[0];

        Console.Write("Enter the first number: ");
        float first = float.Parse(Console.ReadLine());

        Console.Write("Enter the second number: ");
        float second = float.Parse(Console.ReadLine());

        float result = 0;
        if (opr == '+')
            result = first + second;
        else if (opr == '-')
            result = first - second;
        else if (opr == '/')
            result = first / second;
        else if (opr == '*')
            result = first * second;

        Console.WriteLine($"The result of '{first} {opr} {second}' is: {result}");
    }
}    
      

Все було зрозуміло?

Секція 1. Розділ 4
some-alt