vb考试的一些小程序代码? 5
1个回答
展开全部
001求2的n次幂,n的值通过键盘输入
Private Sub Command1_Click()
Dim i As Integer, j As Integer, begin As Integer
begin = 7
For i = 1 To 4
Print Tab(begin);
For j = 1 To 2 * i - 1
Print "1 ";
Next j
Print
begin = begin - 2
Next i
End Sub
002.求公式:e=1/1!+1/2!+1/3!+……+1/n! 直到最后一项小于10^(-6)时e的值。
(注:程序代码写在命令按钮Command1的Click事件中。)
Private Sub Command1_Click()
Dim e As Double, nj As Double
Dim n As Long, i As Long
e = 0
n = 1
Do
nj = 1
For i = 1 To n
nj = nj * i
Next i
e = e + 1 / nj
n = n + 1
Loop Until (1 / nj) < 0.000001
Print e
End Sub
003编写程序计算并输出下面级数前n项(n=10)的部分和 用循环
1!+2!+3!+4!+....+n!
Private Sub Form_Click()
Dim sum As Long, jc As Long
Dim i As Integer, n As Integer
sum = 0
For n = 1 To 10
jc = 1
For i = 1 To n
jc = jc * i
Next i
sum = sum + jc
Next n
Print sum
End Sub
004编写程序,计算并输出下面级数前n项(n=10)的部分和要求用循环做1!+2!+3!+....+N!
Private Sub Form_Click()
Dim sum As Long, jc As Long
Dim i As Integer, n As Integer
sum = 0
n = Val(InputBox(""))
For n = 1 To n
jc = 1
For i = 1 To n
jc = jc * i
Next i
sum = sum + jc
Next n
Print sum
End Sub
005 *
***
*****
*******
*****
***
*
Private Sub Form_Click()
Dim i As Integer, begin As Integer
begin = 5
For i = 1 To 5
Print Tab(begin);
For j = 1 To 2 * i - 1
Print "*";
Next j
Print
begin = begin - 1
Next i
begin = 2
For i = 1 To 4
Print Tab(begin);
For j = 1 To 9 - 2 * i
Print "*";
Next j
Print
begin = begin + 1
Next i
End Sub
006编写程序,单击窗体时在窗体上输出如下所示的图形。
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
Private Sub Form_Click()
Dim begin As Integer, n As Integer, i As Long
begin = 9
For i = 1 To 5
Print Tab(begin);
For n = 1 To 2 * i - 1
Print "*";
Next n
Print
begin = begin - 2
Next i
begin = 3
For i = 1 To 4
Print Tab(begin);
For n = 1 To 9 - 2 * i
Print "*";
Next n
Print
begin = begin + 2
Next i
End Sub
007编写程序,输出下列三角形总行数通过键盘输入用循环做
111111111
1111111
11111
111
1
Private Sub Command1_Click()
i = Val(InputBox(""))
begin = 1
For i = i To 1 Step -1
Print Tab(begin);
For j = 1 To 2 * i - 1
Print "1 ";
Next j
Print
begin = begin + 2
Next i
End Sub
008勾股定理中三个数关系a2+b2=c2编写程序,输出100以内满足上述关系的整数组合,
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
Dim cnt
cnt = 0
For a = 1 To 99
For b = 1 To 99
For c = 1 To 99
If (a * a + b * b = c * c) Then
Print a; ""; b; ""; c; "";
cnt = cnt + 1
If cnt Mod 6 = 0 Then Print
End If
Next c
Next b
Next a
End Sub
009
编写程序 输出100 到1000 之间的所有质数,并给出100 -1000 之间质数的个数以及和
Private Sub Form_Click()
Dim i As Long, n As Long
Dim cnt As Long, sum As Long
Dim flag As Boolean
cnt = 0
sum = 0
For n = 100 To 1000
flag = True
For i = 2 To n - 1
If n Mod i = 0 Then
flag = False
Exit For
End If
Next i
If flag = True Then
cnt = cnt + 1
sum = sum + n
Print n,
If cnt Mod 10 = 0 Then Print
End If
Next n
Print
Print cnt, sum
End Sub
0010输出下列三角形 总行数在窗体单击事件通过键盘输入用循环
1
333
55555
7777777
Private Sub Form_Click()
Dim i As Long, j As Long
Dim n As Long, begin As Long
n = Val(InputBox(""))
begin = 2 * n - 1
For i = 1 To n
Print Tab(begin);
For j = 1 To (2 * i - 1)
Print Trim(2 * i - 1); " ";
Next j
Print
begin = begin - 2
Next i
End Sub
0011求1000 以内分别被3 5 7 除余数都为1的正整数的个数以及其和
Private Sub Command1_Click()
Dim n As Integer, sum As Double
Dim cnt As Integer
sum = 0
cnt = 0
For n = 1 To 1000
If (n Mod 3 = 1) And (n Mod 5 = 1) And (n Mod 7 = 1) Then
Print n
cnt = cnt + 1
sum = sum + n
End If
Next n
Print
Print "共"; cnt; "个"
Print "和为"; sum
End Sub
0012给定年号与月份,判断该年是否闰年,并根据给出的月份来判断是什么季节。
(闰年的条件是:年号能被4整除但不能被100整除,或者能被400整除)
Private Sub Command1_Click()
Dim year As Long
Dim month As Integer
year = Int(Val(InputBox("请输入年份")))
month = Int(Val(InputBox("请输入月份")))
If (year Mod 4 = 0 And year Mod 100 <> 0) Or (year Mod 400 = 0) Then
Print year; "年是闰年,";
Else
Print year; "不是闰年,";
End If
Select Case month
Case 1, 2, 3
Print month; "月是春季"
Case 4, 5, 6
Print month; "月是夏季"
Case 7, 8, 9
Print month; "月是秋季"
Case 10, 11, 12
Print month; "月是冬季"
End Select
End Sub
0013输出用循环
G
FG
EFG
DEFG
CDEFG
BCDEFG
ABCDEFG
Private Sub Command1_Click()
Dim code1 As Integer, code2 As Integer
For code1 = Asc("G") To Asc("A") Step -1
For code2 = code1 To Asc("G")
Print Chr(code2);
Next code2
Print
Next code1
End Sub
00141/(1*2)+1/(2*3)+1/(3*4)+.....+1/(n*(n+1)...
Private Sub Command1_Click()
Dim sum As Double, n As Long
sum = 0
For n = 1 To 30
sum = sum + 1 / (n * (n + 1))
Next n
Print sum
End Sub
0015 2
333
55555
8888888
1313131313131313131
Private Sub Command1_Click()
Dim n1 As Long, n2 As Long, n3 As Long
Dim cnt As Long, begin As Long
Dim i As Long, j As Long
cnt = Int(Val(InputBox("")))
begin = 4 * (2 * cnt - 1) / 2
n1 = 1
n2 = 1
For i = 1 To cnt
Print Tab(begin);
n3 = n1 + n2
For j = 1 To 2 * i - 1
Print Trim(n3); Spc(4 - Len(Trim(n3)));
Next j
begin = begin - 4
n1 = n2
n2 = n3
Next i
End Sub
0016奇数位减偶数位差
Private Sub Command1_Click()
Dim sum1 As Double, sum2 As Double
Dim str As String, i As Long
str = InputBox("")
Print str
sum1 = 0
sum2 = 0
For i = 1 To Len(str)
If i Mod 2 = 1 Then
sum1 = sum1 * 10 + Val(Mid(str, i, 1))
Else
sum2 = sum2 * 10 + Val(Mid(str, i, 1))
End If
Next i
Print sum1; "-"; Sum; ""; sum1 - sum2
End Sub
017序列:1,1/2,2/3,3/5,5/8,8/13,13/21......求这个数列的前20项之和
Private Sub Command1_Click()
Dim sum As Double
Dim a1 As Long, a2 As Long, a3 As Long
Dim b1 As Long, b2 As Long, b3 As Long
Dim i As Long
a1 = 1: a2 = 1
b1 = 1: b2 = 2
sum = a1 / b1 + a2 / b2
For i = 2 To 20
a3 = a1 + a2
b3 = b1 + b2
sum = sum + a3 / b3
a1 = a2
a2 = a3
b1 = b2
b2 = b3
Next i
Print sum
End Sub
0018 1
333
55555
7777777
999999999
Private Sub Command1_Click()
Dim i As Integer, j As Integer
For i = 1 To 5
Print Tab(20 - 3 * i);
For j = 1 To i * 2 - 1
Print i * 2 - 1;
Next j
Print
Next i
End Sub
0019字符倒置
Private Sub Command1_Click()
Dim str1 As String, str2 As String
Dim i As Integer, k As Integer
str1 = InputBox("")
str2 = ""
k = Len(str1)
For i = k To 1 Step -1
str2 = str2 + Mid(str1, i, 1)
Next i
Print str2
End Sub
0020求e值 e=1+1/1!+1/2!.....
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim s As Single, h As Single, l As Single, k!
k = 1
s = 1
For i = 1 To 20
k = k * i
s = s + 1 / k
Next i
Print s
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, begin As Integer
begin = 7
For i = 1 To 4
Print Tab(begin);
For j = 1 To 2 * i - 1
Print "1 ";
Next j
begin = begin - 2
Next i
End Sub
002.求公式:e=1/1!+1/2!+1/3!+……+1/n! 直到最后一项小于10^(-6)时e的值。
(注:程序代码写在命令按钮Command1的Click事件中。)
Private Sub Command1_Click()
Dim e As Double, nj As Double
Dim n As Long, i As Long
e = 0
n = 1
Do
nj = 1
For i = 1 To n
nj = nj * i
Next i
e = e + 1 / nj
n = n + 1
Loop Until (1 / nj) < 0.000001
Print e
End Sub
003编写程序计算并输出下面级数前n项(n=10)的部分和 用循环
1!+2!+3!+4!+....+n!
Private Sub Form_Click()
Dim sum As Long, jc As Long
Dim i As Integer, n As Integer
sum = 0
For n = 1 To 10
jc = 1
For i = 1 To n
jc = jc * i
Next i
sum = sum + jc
Next n
Print sum
End Sub
004编写程序,计算并输出下面级数前n项(n=10)的部分和要求用循环做1!+2!+3!+....+N!
Private Sub Form_Click()
Dim sum As Long, jc As Long
Dim i As Integer, n As Integer
sum = 0
n = Val(InputBox(""))
For n = 1 To n
jc = 1
For i = 1 To n
jc = jc * i
Next i
sum = sum + jc
Next n
Print sum
End Sub
005 *
***
*****
*******
*****
***
*
Private Sub Form_Click()
Dim i As Integer, begin As Integer
begin = 5
For i = 1 To 5
Print Tab(begin);
For j = 1 To 2 * i - 1
Print "*";
Next j
begin = begin - 1
Next i
begin = 2
For i = 1 To 4
Print Tab(begin);
For j = 1 To 9 - 2 * i
Print "*";
Next j
begin = begin + 1
Next i
End Sub
006编写程序,单击窗体时在窗体上输出如下所示的图形。
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
Private Sub Form_Click()
Dim begin As Integer, n As Integer, i As Long
begin = 9
For i = 1 To 5
Print Tab(begin);
For n = 1 To 2 * i - 1
Print "*";
Next n
begin = begin - 2
Next i
begin = 3
For i = 1 To 4
Print Tab(begin);
For n = 1 To 9 - 2 * i
Print "*";
Next n
begin = begin + 2
Next i
End Sub
007编写程序,输出下列三角形总行数通过键盘输入用循环做
111111111
1111111
11111
111
1
Private Sub Command1_Click()
i = Val(InputBox(""))
begin = 1
For i = i To 1 Step -1
Print Tab(begin);
For j = 1 To 2 * i - 1
Print "1 ";
Next j
begin = begin + 2
Next i
End Sub
008勾股定理中三个数关系a2+b2=c2编写程序,输出100以内满足上述关系的整数组合,
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
Dim cnt
cnt = 0
For a = 1 To 99
For b = 1 To 99
For c = 1 To 99
If (a * a + b * b = c * c) Then
Print a; ""; b; ""; c; "";
cnt = cnt + 1
If cnt Mod 6 = 0 Then Print
End If
Next c
Next b
Next a
End Sub
009
编写程序 输出100 到1000 之间的所有质数,并给出100 -1000 之间质数的个数以及和
Private Sub Form_Click()
Dim i As Long, n As Long
Dim cnt As Long, sum As Long
Dim flag As Boolean
cnt = 0
sum = 0
For n = 100 To 1000
flag = True
For i = 2 To n - 1
If n Mod i = 0 Then
flag = False
Exit For
End If
Next i
If flag = True Then
cnt = cnt + 1
sum = sum + n
Print n,
If cnt Mod 10 = 0 Then Print
End If
Next n
Print cnt, sum
End Sub
0010输出下列三角形 总行数在窗体单击事件通过键盘输入用循环
1
333
55555
7777777
Private Sub Form_Click()
Dim i As Long, j As Long
Dim n As Long, begin As Long
n = Val(InputBox(""))
begin = 2 * n - 1
For i = 1 To n
Print Tab(begin);
For j = 1 To (2 * i - 1)
Print Trim(2 * i - 1); " ";
Next j
begin = begin - 2
Next i
End Sub
0011求1000 以内分别被3 5 7 除余数都为1的正整数的个数以及其和
Private Sub Command1_Click()
Dim n As Integer, sum As Double
Dim cnt As Integer
sum = 0
cnt = 0
For n = 1 To 1000
If (n Mod 3 = 1) And (n Mod 5 = 1) And (n Mod 7 = 1) Then
Print n
cnt = cnt + 1
sum = sum + n
End If
Next n
Print "共"; cnt; "个"
Print "和为"; sum
End Sub
0012给定年号与月份,判断该年是否闰年,并根据给出的月份来判断是什么季节。
(闰年的条件是:年号能被4整除但不能被100整除,或者能被400整除)
Private Sub Command1_Click()
Dim year As Long
Dim month As Integer
year = Int(Val(InputBox("请输入年份")))
month = Int(Val(InputBox("请输入月份")))
If (year Mod 4 = 0 And year Mod 100 <> 0) Or (year Mod 400 = 0) Then
Print year; "年是闰年,";
Else
Print year; "不是闰年,";
End If
Select Case month
Case 1, 2, 3
Print month; "月是春季"
Case 4, 5, 6
Print month; "月是夏季"
Case 7, 8, 9
Print month; "月是秋季"
Case 10, 11, 12
Print month; "月是冬季"
End Select
End Sub
0013输出用循环
G
FG
EFG
DEFG
CDEFG
BCDEFG
ABCDEFG
Private Sub Command1_Click()
Dim code1 As Integer, code2 As Integer
For code1 = Asc("G") To Asc("A") Step -1
For code2 = code1 To Asc("G")
Print Chr(code2);
Next code2
Next code1
End Sub
00141/(1*2)+1/(2*3)+1/(3*4)+.....+1/(n*(n+1)...
Private Sub Command1_Click()
Dim sum As Double, n As Long
sum = 0
For n = 1 To 30
sum = sum + 1 / (n * (n + 1))
Next n
Print sum
End Sub
0015 2
333
55555
8888888
1313131313131313131
Private Sub Command1_Click()
Dim n1 As Long, n2 As Long, n3 As Long
Dim cnt As Long, begin As Long
Dim i As Long, j As Long
cnt = Int(Val(InputBox("")))
begin = 4 * (2 * cnt - 1) / 2
n1 = 1
n2 = 1
For i = 1 To cnt
Print Tab(begin);
n3 = n1 + n2
For j = 1 To 2 * i - 1
Print Trim(n3); Spc(4 - Len(Trim(n3)));
Next j
begin = begin - 4
n1 = n2
n2 = n3
Next i
End Sub
0016奇数位减偶数位差
Private Sub Command1_Click()
Dim sum1 As Double, sum2 As Double
Dim str As String, i As Long
str = InputBox("")
Print str
sum1 = 0
sum2 = 0
For i = 1 To Len(str)
If i Mod 2 = 1 Then
sum1 = sum1 * 10 + Val(Mid(str, i, 1))
Else
sum2 = sum2 * 10 + Val(Mid(str, i, 1))
End If
Next i
Print sum1; "-"; Sum; ""; sum1 - sum2
End Sub
017序列:1,1/2,2/3,3/5,5/8,8/13,13/21......求这个数列的前20项之和
Private Sub Command1_Click()
Dim sum As Double
Dim a1 As Long, a2 As Long, a3 As Long
Dim b1 As Long, b2 As Long, b3 As Long
Dim i As Long
a1 = 1: a2 = 1
b1 = 1: b2 = 2
sum = a1 / b1 + a2 / b2
For i = 2 To 20
a3 = a1 + a2
b3 = b1 + b2
sum = sum + a3 / b3
a1 = a2
a2 = a3
b1 = b2
b2 = b3
Next i
Print sum
End Sub
0018 1
333
55555
7777777
999999999
Private Sub Command1_Click()
Dim i As Integer, j As Integer
For i = 1 To 5
Print Tab(20 - 3 * i);
For j = 1 To i * 2 - 1
Print i * 2 - 1;
Next j
Next i
End Sub
0019字符倒置
Private Sub Command1_Click()
Dim str1 As String, str2 As String
Dim i As Integer, k As Integer
str1 = InputBox("")
str2 = ""
k = Len(str1)
For i = k To 1 Step -1
str2 = str2 + Mid(str1, i, 1)
Next i
Print str2
End Sub
0020求e值 e=1+1/1!+1/2!.....
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim s As Single, h As Single, l As Single, k!
k = 1
s = 1
For i = 1 To 20
k = k * i
s = s + 1 / k
Next i
Print s
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询