Answered # write a program which print power of 2

Discussion Starter pytup AndreRet 362

1

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
```

0

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.

Recommended Articles

Hi guys

I am developing a simple job card creator application with VB.net 2012, using Access 2010 as database.

The code I have generated to write data from a datagridview to the database table is successfully saving the data, but with an error "Parameter @name has no default value". The ...

get the input from user may include

-Bank balance

-Amount invested in business

-Amount given to other business partners as loan

and

-Amount payable to others

Made this in cpp.sh

Why would I need the x=y+1-1;?

x=y; just doesnt seem to work

Why is this?

And I know my coding ...