Exercise
Sum numbers
Objetive
Write a Visual Basic (VB.Net) program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum, as follows:
Number? 5
Total = 5
Number? 10
Total = 15
Number? -2
Total = 13
Number? 0
Finished"
Code
' Import the System namespace to use Console class
Imports System
Public Class Program
' Main subroutine, entry point of the program
Public Shared Sub Main()
' Initialize the total sum as 0
Dim total As Integer = 0
Dim number As Integer
' Start a loop to repeatedly ask the user for numbers
Do
' Prompt the user for a number
Console.Write("Number? ")
number = Integer.Parse(Console.ReadLine())
' Add the entered number to the total sum
total += number
' Display the updated total
Console.WriteLine("Total = " & total)
Loop While number <> 0 ' Continue until the user enters 0
' Display the message indicating the end of the process
Console.WriteLine("Finished")
End Sub
End Class