Basic Programming C# Examples

C# Program to Perform a Simple Arithmetic Calculation

The Program in C# Sharp which is a Menu-Driven Program to perform a simple calculation inputted by user is given below:

using System;

namespace MenuDrivenCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Hello Codeauri Family,enter number below to perform the simple arithemtic calculation: ");
                Console.WriteLine("1. Addition");
                Console.WriteLine("2. Subtraction");
                Console.WriteLine("3. Multiplication");
                Console.WriteLine("4. Division");
                Console.WriteLine("5. Exit");
                Console.WriteLine("Enter your choice (1-5):");

                int choice = int.Parse(Console.ReadLine());
                if (choice == 5)
                    break;

                Console.WriteLine("Enter two numbers:");
                double num1 = double.Parse(Console.ReadLine());
                double num2 = double.Parse(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        Console.WriteLine("Result: " + (num1 + num2));
                        break;
                    case 2:
                        Console.WriteLine("Result: " + (num1 - num2));
                        break;
                    case 3:
                        Console.WriteLine("Result: " + (num1 * num2));
                        break;
                    case 4:
                        Console.WriteLine("Result: " + (num1 / num2));
                        break;
                    default:
                        Console.WriteLine("Invalid choice");
                        break;
                }
                Console.WriteLine("\n");
            }
        }
    }
}

Output:

Hello Codeauri Family,enter number below to perform the simple arithemtic calculation:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Exit
    Enter your choice (1-5):
    1
    Enter two numbers:
    9099
    1099
    Result: 10198
    Hello Codeauri Family,enter number below to perform the simple arithemtic calculation:
  6. Addition
  7. Subtraction
  8. Multiplication
  9. Division
  10. Exit
    Enter your choice (1-5):

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line using System; is a directive that imports the System namespace in the program.
  2. The namespace keyword defines a container for a set of related classes and is used to organize code into logical units. In this case, the namespace is named MenuDrivenCalculator.
  3. The class keyword defines a class. In this case, the class is named Program.
  4. The Main method is the entry point of the program. It is called when the program starts.
  5. Inside the Main method, there is a while loop that continues to run until the user enters the value 5. This loop presents the menu of options to the user and allows them to repeatedly perform calculations.
  6. The Console.WriteLine method writes a line of text to the console. In this code, it is used to display the menu of options, the result of the calculation, and other messages to the user.
  7. The int.Parse method converts a string representation of a number to an integer. It is used to parse the user’s choice of operation.
  8. The if statement checks if the value of choice is equal to 5. If it is, the break statement is executed and the program exits the loop.
  9. The double.Parse method converts a string representation of a number to a double-precision floating-point number. It is used to parse the two numbers entered by the user.
  10. The switch statement is used to perform the calculation based on the user’s choice of operation. It has a case for each operation and a default case for invalid inputs.
  11. The Console.WriteLine("\n"); statement writes a blank line to the console, which is used to separate the outputs of each iteration of the loop.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X