Block of Code

Practical Examples for Programmers

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

Length Conversion

In Visual Basic, write a program to convert a U.S. Customary System length in miles, yard, feet, and inches to a Metric System length in kilometers, meters, and centimeters. A sample run is shown below.  After the numbers of miles, yards, feet, and inches are read from the text boxes, the length should be converted entirely to inches and then divided by 39.37 to obtain the value in meters.  The Int function should be used to break the total number of meters into a whole number of kilometers and meters.  The number of centimeters should be displayed to one decimal place.  The needed formulas are as follows:

total inches = 63360*miles + 36*yards + 12*feet + inches
total meters = total inches/39.37
kilometers = Int(meters/1000)

Length Conversion
Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmLengthConversion Conversion Form Holds Controls
 txtMiles TextBox Captures Miles
 txtYards TextBox Captures Yards
 txtFeet TextBox Captures Feet
 txtInches TextBox Captures Inches
 btnConvert Convert to Metric Button Triggers event to calculate metric lengths
 lstOutput ListBox Displays length in metric unites

Write the code:

' Project: Length Conversion
' Programmer: Your Name Here
' Date: July 17, 2014
' Description: Converts US length to metric length

Public Class frmLengthConversion

   Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
      Dim miles As Integer = CInt(txtMiles.Text)
      Dim yards As Integer = CInt(txtYards.Text)
      Dim feet As Integer = CInt(txtFeet.Text)
      Dim inches As Integer = CInt(txtInches.Text)
      Dim totalInches As Double = 63360 * miles + 36 * yards + 12 * feet + inches
      Dim totalMeters As Double = totalInches / 39.37
      Dim kilometers As Integer = CInt(totalMeters / 1000)
      Dim meters As Integer = CInt(totalMeters Mod 1000)
      Dim centimeters As Double = (totalMeters * 100) Mod 100
      lstOutput.Items.Add("The metric length is")
      lstOutput.Items.Add(kilometers & " kilometers")
      lstOutput.Items.Add((meters).ToString("N0") & " meters")
      lstOutput.Items.Add((centimeters).ToString("N1") & " centimeters.")
   End Sub

End Class

Secondary Sidebar

This is the secondary sidebar

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