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:
- Addition
- Subtraction
- Multiplication
- Division
- 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: - Addition
- Subtraction
- Multiplication
- Division
- Exit
Enter your choice (1-5):
Pro-Tips💡
Here are the step by step execution of above program:
- The first line
using System;
is a directive that imports theSystem
namespace in the program. - 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 namedMenuDrivenCalculator
. - The
class
keyword defines a class. In this case, the class is namedProgram
. - The
Main
method is the entry point of the program. It is called when the program starts. - Inside the
Main
method, there is awhile
loop that continues to run until the user enters the value5
. This loop presents the menu of options to the user and allows them to repeatedly perform calculations. - 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. - 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. - The
if
statement checks if the value ofchoice
is equal to5
. If it is, thebreak
statement is executed and the program exits the loop. - 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. - The
switch
statement is used to perform the calculation based on the user’s choice of operation. It has acase
for each operation and a default case for invalid inputs. - 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.