Sum numbers Learn programming Visual Basic (VB.net)



Lesson:

Flow Control


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:

Imports System
Public Class Exercise25
    Public Shared Sub Main()
        Dim number As Integer
        Dim sum As Integer = 0

        Do
            Console.Write("Number: ")
            number = Convert.ToInt32(Console.ReadLine())
            sum = number + sum
            If number <> 0 Then Console.WriteLine("Total: {0}", sum)
        Loop While number <> 0

        Console.WriteLine("Finished")
    End Sub
End Class



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.