Write a program to create a Simple Calculator in Visual Basic (VB.NET)

Write a program to create a Simple Calculator in Visual Basic (VB.NET). In this article, we will write a program to create a calculator in Visual Basic (VB.NET). Here, we will create a simple calculator using ‘select case’. Here we perform addition, subtraction, multiplication, and division operation.

Simple Calculator in Visual Basic (VB.NET)

Following is the source code to create a simple calculator using ‘select case’.

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

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 4
                result = num1 / num2
            Case Else
                Console.WriteLine("Invalid choice")
        End Select

        Console.WriteLine("Result:  {0}", result)
    End Sub
    
End Module

Output

Explanation

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

we defined “select case” to perform the operation. Here we select 3rd case and perform the multiplication operation and print the result on the console screen.

Working

In the above program, first, the compiler check for the main method then it prints the choices which are given in the console.

  • After that it prints enter choice which is our select case variable whose name is ‘choice’ we take input in this variable by “choice = Integer.Parse(Console.ReadLine())”.
  • The readline() is used to take user input from the keyboard and the number we have selected if any given select case is matched with it so that select case statement body would run otherwise the default case would run that shows “invalid choice”.
  • For Example: if we input 2 in the choice then we need to enter two inputs 9 and 5. Then the result will show as 4.
  • A Select Case block evaluates choice to determine which operator has been invoked and then applies the operator to num1 and num2 to calculate the value of the result.

In this way, we learned how to write a program to create a Simple Calculator in Visual Basic (VB.NET).