For循环习题
1. 求1+2+3+……+10累加和
Private Sub Command1_Click()
For i = 1 To 10
s = s + i
Next
Form1.Print s
End Sub
2. 求100~200之间能够同时被5和7整除的数的和
Private Sub Command1_Click()
For i = 100 To 200
If i Mod 5 = 0 And i Mod 7 = 0 Then
s = s + i
End If
Next
Print s
End Sub
3. 求6!阶乘
Private Sub Command1_Click()
printform
t = 1
For i = 1 To 6
t = t * i
Next
Print t
End Sub
4. 求阶乘累加和,1!+2!+ (6)
Private Sub Command1_Click()
t = 1
For i = 1 To 6
t = t * i
s = s + t
Next
Print s
End Sub
5. 水仙花数
提示:一个三位数字的个位、十位、百位的立方和恰好等于数字本身(153 = 13 + 53 + 33)Private Sub Command1_Click()
For i = 100 To 999
bw = i \ 100
sw = (i - bw * 100) \ 10
gw = i Mod 10
If bw ^ 3 + sw ^ 3 + gw ^ 3 = i Then Print i
Next
End Sub
6. 判断一个数是不是素数
Private Sub Command1_Click()
x% = InputBox("请输入一个数字")
For i = 2 To x - 1
If x Mod i = 0 Then Exit For
Next
If i > x - 1 Then
Print x; "是素数"
Else
Print x; "不是素数"
End If
End Sub
7. 求任意两个数的最大公约数和最小公倍数
Private Sub Command1_Click()
Dim x%, y%, min%
x = InputBox("请输入数字")
y = InputBox("请输入数字")
If x > y Then
min = y
Else
min = x
End If
For i = min To 1 Step -1
If x Mod i = 0 And y Mod i = 0 Then
Print i '最大公约数
Exit For
End If
Next
Print x * y / i '最小公倍数End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。