﻿ C# Sharp exercises: A menu-driven program for a simple calculator - w3resource

# C# Sharp Exercises: A menu-driven program for a simple calculator

## C# Sharp Conditional Statement: Exercise-25 with Solution

Write a program in C# Sharp which is a Menu-Driven Program to perform a simple calculation.

Sample Solution:-

C# Sharp Code:

``````using System;
public class Exercise25
{
public static void Main()
{
int num1,num2,opt;

Console.Write("\n\n");
Console.Write("A menu driven program for a simple calculator:\n");
Console.Write("------------------------------------------------");
Console.Write("\n\n");

Console.Write("Enter the first Integer :");
Console.Write("Enter the second Integer :");

Console.Write("\nHere are the options :\n");
Console.Write("\nInput your choice :");

switch(opt) {
case 1:
Console.Write("The Addition of  {0} and {1} is: {2}\n",num1,num2,num1+num2);
break;

case 2:
Console.Write("The Substraction of {0}  and {1} is: {2}\n",num1,num2,num1-num2);
break;

case 3:
Console.Write("The Multiplication of {0}  and {1} is: {2}\n",num1,num2,num1*num2);
break;

case 4:
if(num2==0) {
Console.Write("The second integer is zero. Devide by zero.\n");
} else {
Console.Write("The Division of {0}  and {1} is : {2}\n",num1,num2,num1/num2);
}
break;

case 5:
break;

default:
Console.Write("Input correct option\n");
break;
}
}
}
```
```

Sample Output:

```A menu driven program for a simple calculator:
------------------------------------------------
Enter the first Integer :20
Enter the second Integer :4

Here are the options :
2-Substraction.
3-Multiplication.
4-Division.
5-Exit.

Input your choice :4
The Division of 20  and 4 is : 5
```

Flowchart:

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

﻿

New Content: Composer: Dependency manager for PHP, R Programming