In: Computer Science
All of these programs need IO. So you must use appropriate
Visual Studio solution as
the template (windows32 has been provided in the book’s website and
you supposed
to know how to use it as it was required in the previous
assignment)
Write an assembly language program to calculate the
following.
(((((20 + 21) × 22) + 23)×24)+25 : : :) + 2n
Hint: Note that when n is even, carrying result is multiplied by
2n. When n is odd,
carrying result is added to 2n (above formula shows the case where
n is odd).
As an example, when n = 5, it should print 352.
You need to have a dialog box to read the value of n.
You need to check for n < 0 and display an appropriate error
message.
As n increases, at one point, you will not be able to hold the
result in a 32-
bit register. You should check for it (probably using jo
instruction), display an
appropriate error message.
Valid result should be displayed using a message box.
'NOTE: I DON'T GET THE FORMULA PROPERLY, I THINK, IT'S NEED TO RECHECKED.
'NOTE: I'VE MADE THE LOGIC. YOU ENTER THE STARTING POINT AS PER REQUIREMENT
'NOTE: if n = 5. it shows 35 (according to you, the result is 352. i think it was a typing mistake.)
' if n = 5 the formula will be. (((((0+1)x2)+3)x4)+5) + 2x5 = 35
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Please Enter a Number") 'checks the test box is empty or
not
Else
Dim n As Long
n = Long.Parse(TextBox1.Text) 'store the value of text box into
n
n = Val(n)
If n < 0 Then 'checks the n is positive or not
MsgBox("Error: Please Enter a Valid Number")
Else
Dim result As Long
Dim nxt As Long
result = 0
For i As Long = 0 To n - 1 'ENTER YOUR STARTING POINT AS YOUR
REQUIREMENT
nxt = i + 1
If (i Mod 2 = 0) Then 'checks whether the i is odd or even
result = result + nxt
Else
result = result * nxt
End If
Next
If (n Mod 2 = 0) Then
result = result * (2 * n) 'multipling when n is even
Else
result = result + 2 * n 'adding when n is odd
End If
MessageBox.Show(result, "Result")
End If
End If
End Sub
End Class