Home >>VB.Net Programs >VB.Net program to create a simple calculator using 'select case'

VB.Net program to create a simple calculator using 'select case'

Write a VB.Net program to create a simple calculator using 'select case'

Using' select case 'here, we can create a basic calculator, where we perform addition, subtraction, multiplication, and division operations.

Program :

Below is the source code for creating a simple calculator using "select case". The program given is compiled and successfully executed.

Example


Module Module1
    Sub Main()
        Dim choice As Integer
        Dim num1 As Integer = 0
        Dim num2 As Integer = 0
        Dim result As Integer = 0
        Console.WriteLine("*************************")
        Console.WriteLine("     1: Addition")
        Console.WriteLine("     2: Subtraction")
        Console.WriteLine("     3: Multiplication")
        Console.WriteLine("     4: Division")
        Console.WriteLine("*************************")
        Console.Write("Enter choice: ")
        choice = Integer.Parse(Console.ReadLine())
        Console.Write("Enter number1: ")
        num1 = Integer.Parse(Console.ReadLine())
        Console.Write("Enter number2: ")
        num2 = Integer.Parse(Console.ReadLine())
        Select Case choice
            Case 1
                result = num1 + num2
            Case 2
                result = num1 - num2
            Case 3
                result = num1 * num2
            Case 3
                result = num1 / num2
            Case Else
                Console.WriteLine("Invalid choice")
        End Select
        Console.WriteLine("Result:  {0}", result)
    End Sub
End Module
	
Output:
*************************
1: Addition
2: Subtraction
3: Multiplication
4: Division
*************************
Enter choice: 3
Enter number1: 3
Enter number2: 6
Result: 18

Explanation:

We created a Module1 module in the program above that contains the Main() method. We created four variables, num1, num2, and result, in the Main() method.

We have specified "select case" in the program above to execute the operation. We select the 3rd case here, perform the multiplication operation and print the result on the screen of the console.


No Sidebar ads