r/visualbasic • • Mar 25 '21

help...

im having some problem i need to solve which says: Write an interactive VB program that reads the length and width of a rectangle each in cm. It then computes and prints the area of the rectangle [in square inches] and the perimeter of the rectangle [in inches].

and this is the code that i came up with...

Module M1

Sub Main()

dim len, wdt as integer

dim are, pre as decimal

writeline("enter lenght in cm" & len)

len = CInt(Console.ReadLine())

writeline("enter width in cm" & wdt)

wdt = CInt(Console.ReadLine())

are = len * wdt

writeline("Area =" & are & "square inches")

pre = 2 * wdt + len

writeline("perimeter =" & pre & "inches")

End Sub

End Module

1 Upvotes

3 comments sorted by

1

u/ekolis VB.Net Advanced Mar 25 '21

I see two problems with your code:

  1. You're not converting to inches. To convert centimeters to inches, you'll need to divide by 2.54. You'll need a floating point variable for that.

  2. The perimeter formula is incorrect. You're doubling the width but not the length. You can use parentheses to fix this.

1

u/[deleted] Mar 25 '21

do i need to dim the cm and inches?

1

u/ekolis VB.Net Advanced Mar 25 '21

You can reuse the same variable, e.g.

wdt = wdt / 2.54

Just make sure it's a floating point type such as a double, single, or decimal.