write a program which will print power of 2

use loop


2 ^ 0 1
2 ^ 1 2
2 ^ 2 3
2 ^ 3 4

Edited 7 Years Ago by pytup: n/a

write a program which will print power of 2

use loop


2 ^ 0 1
2 ^ 1 2
2 ^ 2 3
2 ^ 3 4

i done this already.

Private Sub Command1_Click()
Dim numbers As Integer
Dim answers As Integer
numbers = 0

Do While numbers < 9
answer = 2 ^ numbers
Print answer
numbers = numbers + 1
Loop

End Sub

The following will help with the computation. If you need help on the printing side, please specify if you are to print to your printer, the form etc. -

Function Power2(ByVal exponent As Long) As Long
    Static res(0 To 31) As Long
    Dim i As Long
    
    ' Raise 2 to a power
    ' the exponent must be in the range [0,31] or change the Static Res above.
    
    ' rule out errors
    If exponent < 0 Or exponent > 31 Then Err.Raise 5
    
    ' initialize the array at the first call
    If res(0) = 0 Then
        res(0) = 1
        For i = 1 To 30
            res(i) = res(i - 1) * 2
        Next
        ' this is a special case
        res(31) = &H80000000
    End If
    
    ' return the result
    Power2 = res(exponent)
    Text2.Text = Power2
        
End Function

Private Sub Command1_Click()

'First check to see if an inteher has been added to textbox.
If Text1.Text = "" Then
    MsgBox "Please add a number to compute exponent.", vbOKOnly + vbExclamation, "Add Integer"
    
    Text1.SetFocus
    Exit Sub
'Check if data entered is an integer.
ElseIf Not IsNumeric(Text1.Text) Then
    MsgBox "Only numbers accepted as an entry.", vbOKOnly + vbExclamation, "Add Integer"
        Else
    Call Power2(Text1.Text)
End If
End Sub

Hope this helps...

This question has already been answered. Start a new discussion instead.