Answered # write a program which print power of 2

Discussion Starter pytup AndreRet 362

0

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!

I have two tables, I will call them first-half and second-half (of the year)

```
Table: first
id jan feb mar apr
-----------------------
2 80 90 70 60
3 50 40 60 30
Table: second
id jan feb mar apr
-----------------------
2 50 70 60 90
3 80 60 50 ...
```

I'm trying to figure out how do i print a distinct enum value by using a conditional statement in my construction of array.

I have a enum declaration

`enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};`

And i construct my array using a voidptr ...

I'm new to python, for my project I need to send mail from python.. I'm getting the following error can someone please help me how to get out of it..

```
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for ...
```