Exercise
Many divisions
Objetive
Write a Visual Basic (VB.Net) program that asks the user for two numbers and displays their division and remainder of the division. If 0 is entered as the second number, it will warn the user and end the program if 0 is entered as the first number. Examples:
First number? 10
Second number? 2
Division is 5
Remainder is 0
First number? 10
Second number? 0
Cannot divide by 0
First number? 10
Second number? 3
Division is 3
Remainder is 1
First number? 0
Bye!
Code
' Importing necessary namespaces
Imports System
Public Class Program
Public Shared Sub Main()
' Declare variables for the two numbers entered by the user
Dim firstNumber As Integer
Dim secondNumber As Integer
' Ask the user to input the first number
Console.WriteLine("First number?")
firstNumber = Convert.ToInt32(Console.ReadLine())
' Check if the first number is 0, then exit the program
If firstNumber = 0 Then
Console.WriteLine("Bye!")
Return ' Exit the program if the first number is 0
End If
' Ask the user to input the second number
Console.WriteLine("Second number?")
secondNumber = Convert.ToInt32(Console.ReadLine())
' Check if the second number is 0 and handle the division by 0 case
If secondNumber = 0 Then
Console.WriteLine("Cannot divide by 0")
Else
' Calculate the division and remainder
Dim division As Integer = firstNumber \ secondNumber
Dim remainder As Integer = firstNumber Mod secondNumber
' Display the results of the division and remainder
Console.WriteLine("Division is " & division)
Console.WriteLine("Remainder is " & remainder)
End If
End Sub
End Class