Give change Learn programming Visual Basic (VB.net)

Lesson:

Flow Control


Exercise:

Give change


Objetive:

Create a Visual Basic (VB.net) program to give change for a purchase, using the largest possible coins (or bills). Suppose we have an unlimited amount of coins (or bills) of 100, 50, 20, 10, 5, 2, and 1, and there are no decimals. Therefore, the execution could be something like this:

Price? 44
Paid? 100
Your change is 56: 50 5 1
Price? 1
Paid? 100
Your change is 99: 50 20 20 5 2 2


Code:

Imports System
Public Class Exercise47
    Public Shared Sub Main()
        Dim price, paid, change As Integer
        Console.Write("Price? ")
        price = Convert.ToInt32(Console.ReadLine())
        Console.Write("Paid? ")
        paid = Convert.ToInt32(Console.ReadLine())
        change = paid - price
        Console.Write("Your change is {0}: ", change)

        While change > 0

            If change >= 50 Then
                Console.Write("50 ")
                change -= 50
            Else

                If change >= 20 Then
                    Console.Write("20 ")
                    change -= 20
                Else

                    If change >= 10 Then
                        Console.Write("10 ")
                        change -= 10
                    Else

                        If change >= 5 Then
                            Console.Write("5 ")
                            change -= 5
                        Else

                            If change >= 2 Then
                                Console.Write("2 ")
                                change -= 2
                            Else
                                Console.Write("1 ")
                                change -= 1
                            End If
                        End If
                    End If
                End If
            End If
        End While

        Console.WriteLine()
    End Sub
End Class

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