Calculator - if Learn programming Visual Basic (VB.net)

Lesson:

Basic Data Types


Exercise:

Calculator - if 57


Objetive:

Write a Visual Basic (VB.net) program which asks the user for two numbers and an operation to perform on them (+,-,*,x,/) and displays the result of that operation, as in this example:

Enter first number: 5
Enter operation: +
Enter second number: 7
5+7=12

Note: you MUST use "if", not "switch"
Enter a number: 5
Sum = 5
Enter a number: 3
Sum = 8
Enter a number: end
The numbers are: 5 3
The sum is: 8


Code:

Imports System
Public Class exercise53
    Public Shared Sub Main()
        Dim a, b As Integer
        Dim operation As Char
        Console.Write("Enter first number: ")
        a = Convert.ToInt32(Console.ReadLine())
        Console.Write("Enter operation: ")
        operation = Convert.ToChar(Console.ReadLine())
        Console.Write("Enter second number: ")
        b = Convert.ToInt32(Console.ReadLine())

        If operation = "+"c Then
            Console.WriteLine("{0} + {1} = {2}", a, b, a + b)
        ElseIf operation = "-"c Then
            Console.WriteLine("{0} - {1} = {2}", a, b, a - b)
        ElseIf (operation = "x"c) OrElse (operation = "*"c) Then
            Console.WriteLine("{0} * {1} = {2}", a, b, a * b)
        ElseIf operation = "/"c Then
            Console.WriteLine("{0} / {1} = {2}", a, b, a / b)
        Else
            Console.WriteLine("Wrong Character")
        End If
    End Sub
End Class