Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Change

In Visual Basic, write a program to make change for an amount of money from 0 through 99 cents input by the user. The output of the program should show the number of coins from each denomination used to make change as shown in the image below.

Design the Interface:

First we need to design the interface.  The following image shows an example of what the form might look like when the program runs.

Change

Initially, when the program is loaded, the form “Change” should appear. Once the program is run, the form should be ready for entry. While the program is running, if the user clicks on the “Determine Composition of Change” button, the display will show the change in various US coins composition.

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
frmChange Change Form Holds Controls
txtAmount Text Box Captures amount of change
btnDetermine  Determine Composition of Change Button Triggers event for displaying change composition
txtQuarters Text Box * Displays # of quarters
txtDimes Text Box * Displays # of dimes
txtNickels Text Box * Displays # of nickels
txtPennies Text Box * Displays # of pennies

* The ReadOnly property for this control should be set to true because only the program code should be editing this field.  The user should not be permitted to enter anything in this field.

Write the Code:

' Project: Change
' Programmer: Your Name Here
' Date: February 2, 2014
' Description: Simulates making change. User inputs the amount of change.
' Progam calculates and displays the number of quarters, dimes, nickels and pennies.

Public Class frmChange

   Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click
      ' Declare and initialize the variables
      Dim amount As Integer = CInt(txtAmount.Text)
      Dim quarters As Integer = 0
      Dim dimes As Integer = 0
      Dim nickels As Integer = 0
      Dim cents As Integer = 0

      ' Calculate the change
      quarters = amount \ 25
      amount -= quarters * 25
      dimes = amount \ 10
      amount -= dimes * 10
      nickels = amount \ 5
      amount -= nickels * 5
      cents = amount

      ' Display the calculated change
      txtQuarters.Text = CStr(quarters)
      txtDimes.Text = CStr(dimes)
      txtNickels.Text = CStr(nickels)
      txtCents.Text = CStr(cents)

   End Sub
End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2023 · Metro Pro Theme on Genesis Framework · WordPress · Log in