所謂的水仙花數(梅花數)是指在三位整數(100到999之間)中,百位數、十位數、個位數的立方和等于它本身,如153=1^3+5^3+3^3。程序代碼如下:
Private Sub Command1_Click() Dim i As Integer, s As Integer Dim a As Integer, b As Integer, c As Integer Print "100到999所有水仙花數(也叫梅花數):"
; For i = 100 To 999 a = i \ 100 '取百位數 b = i \10 Mod 10 '或 b = i Mod 100 \10 取十位數 c = i Mod 10 ‘取個位數 s = a ^ 3 + b ^ 3 + c ^ 3 '水仙花數的判斷依據 If s = i Then Print i; End If Next iEnd Sub運行結果:100到999所有水仙花數(也叫梅花數): 153 370 371 407