VB 编程序找出1到1000之内的完数,并打印出它的因子
展开全部
完数,完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。如果一个数恰好等于它的因子之和,则称该数为“完全数”。
VB可是使用Mod运算符求余和嵌套循环来找出完数。
Mod 运算符,用来对两个数作除法并且只返回余数。
可以将一个 For...Next 循环放置在另一个 For...Next
循环中,组成嵌套循环。不过在每个循环中的 counter 要使用不同的变量名。
实现代码:
Private Sub Form_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
For i = 1 To 1000 '指定循环终点
l = 0
For j = 1 To i - 1
If i Mod j = 0 Then l = l + j
Next j
If l = i Then
Print i & "是完数,因子:";
For k = 1 To l - 1
If l Mod k = 0 Then Print k;
Next k
End If
Next i
End Sub
展开全部
修改的别人的,但是能运行成功。不过怎么就3个完数?呵呵,白折腾半天。
Option Explicit
'一个按钮里调用的过程
Private Sub Command1_Click()
OutputWanNumber (1000)
End Sub
'求完数的过程
Sub OutputWanNumber(ByVal UpperBound As Integer)
Dim i As Integer
Dim j As Integer
Dim s As Integer
Dim k As Integer
For i = 1 To UpperBound
s = 0
For j = 1 To i - 1
If i Mod j = 0 Then s = s + j
Next j
If s = i Then
Print i & "是完数\因子:";
For k = 1 To s - 1
If s Mod k = 0 Then Print k;
Next k
Print
End If
Next i
End Sub
Option Explicit
'一个按钮里调用的过程
Private Sub Command1_Click()
OutputWanNumber (1000)
End Sub
'求完数的过程
Sub OutputWanNumber(ByVal UpperBound As Integer)
Dim i As Integer
Dim j As Integer
Dim s As Integer
Dim k As Integer
For i = 1 To UpperBound
s = 0
For j = 1 To i - 1
If i Mod j = 0 Then s = s + j
Next j
If s = i Then
Print i & "是完数\因子:";
For k = 1 To s - 1
If s Mod k = 0 Then Print k;
Next k
End If
Next i
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim i, j, sum
Dim s
For i = 1 To 1000
sum = 0
s = ""
For j = 1 To i \ 2
If i Mod j = 0 Then
sum = sum + j
s = s & j & "+"
End If
Next j
If i = sum Then
Print CStr(i); Tab(5); "="; Left(s, Len(s) - 1)
End If
Next i
Dim s
For i = 1 To 1000
sum = 0
s = ""
For j = 1 To i \ 2
If i Mod j = 0 Then
sum = sum + j
s = s & j & "+"
End If
Next j
If i = sum Then
Print CStr(i); Tab(5); "="; Left(s, Len(s) - 1)
End If
Next i
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询