1输入圆的半径,计算圆的面积,并输出。
Private Sub Command1_Click()
Dim r!, s!
r = InputBox("请输入半径", "输入框")
s = 3.1415926 * r ^ 2
Text1.Text = s
End Sub
输入正方形的边长,计算正方形的面积,并输出。
Private Sub Command1_Click()
Dim a%
a = Text1.Text
s = a * a
Print s
End Sub
计算长方形周长
Private Sub Command1_Click()
Dim m#, n#
m = Val(InputBox("请输入长方形的长"))
n = Val(InputBox("请输入长方形的宽"))
MsgBox (2 * (m + n))
End Sub
输入三个数判断是否能构成三角形,若能计算其面积
Private Sub Command1_Click()
Dim i As Integer, a(2) As Integer, p As Single, s As Single
For i = 0 To 2
a(i) = Val(InputBox("输入第" & i & "条边的边长:"))
Next i
If a(0) + a(1) > a(2) And a(0) + a(2) > a(1) And a(1) + a(2) > a(0) Then
p = (a(0) + a(1) + a(2)) / 2
s = Sqr(p * (p - a(0)) * (p - a(1)) * (p - a(2)))
MsgBox (s)
Else
MsgBox ("不能构成三角形")
End If
End Sub
2输入一个两位数,分别输出它的个位数字和十位数字。
Private Sub Command1_Click()
a = Val(InputBox("", ""))
If a >= 10 And a < 100 Then
c = a \ 10
d = a Mod 10
End If
Print a & "的十位数为:" & c & ",个位数为:" & d
End Sub
输入一个四位数,分别输出它的个位数字、十位数字、百位数字和千位数字。
Private Sub Command1_Click()
a = Val(InputBox("", ""))
If a >= 1000 And a < 10000 Then
c = a \ 1000
d = (a - c * 1000) \ 100
e = (a - c * 1000 - d * 100) \ 10
f = a - c * 1000 - d * 100 - d * 10
End If
Print a & "的千位数为:" & c
Print a & "的百位数为:" & d
Print a & "的十位数为:" & e
Print a & "的个位数为:" & f
End Sub
3裴波那契数列是由计算某类动物繁殖增长量而提出的。数列的前两项是1、1,以后的每一项都是其相邻前两项之和。编写程序求: 数列前30项和FS; (2178308)
Private Sub Command1_Click()
Dim a#(1 To 30), fs&
a(1) = 1
a(2) = 1
For i = 3 To 30
a(i) = a(i - 1) + a(i - 2)
Next
For i = 1 To 30
fs = fs + a(i)
Next
Print fs
End Sub
用Function过程求斐波那契数列的第n项,并调用该函数,显示数列的第5项。斐波那契数列:1,1,2,3,5,8... ...
Dim i As Integer
Private Sub Command1_Click()
n = InputBox("", "")
For i = 1 To n
Print f(i)
Next i
End Sub
Private Function f(a)
If a = 1 Or a = 2 Then
f = 1
Else
f = f(a - 1) + f(a - 2)
End If
End Function
4编写程序,产生一个整型二维数组A(N ,N),(N 取5),数组元素的取值范围为[0,20 ),计算数组中每一列数据的和
Option Base 1
Private Sub Form_Click()
Randomize
Dim a(1 To 5, 1 To 5)
For i = 1 To 3
For j = 1 To 3
a(i, j) = Int(Rnd * 21)
Print a(i, j); " ";
Next j
Print
Next i
c = "每列相加的值是:" & vbCrLf
For i = 1 To 3
For j = 1 To 3
t = t + a(j, i)
Next j
c = c & t & " "
t = 0
Next i
Print c
End Sub
编写程序,建立一个 6×6的整型二维数组(数据由程序调用Rnd函数产生[0,15 )的随机整数数),输出数组中的最大元素。
Option Base 1
vb编程基础知识题库Private Sub Command1_Click()
Dim a(6, 6) As Integer
Randomize
Max = a(1, 1)
For i = 1 To 6
For j = 1 To 6
a(i, j) = Int(Rnd * 16 + 0)
If a(i, j) > Max Then
Max = a(i, j)
End If
Print a(i, j);
n = n + 1
If n Mod 6 = 0 Then
Print
End If
Next j
Next i
Print "最大元素为:"; Max
End Sub
编写一个函数,其参数为一个一维整型数组,其功能为求数组中所有元素的平均值。
Option Base 1
Private Sub Command1_Click()
Dim a()
a() = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
Print "数组中所有元素的平均值为:" & ave(a())
End Sub
Private Function ave(a())
For i = 1 To UBound(a)
Sum = Sum + a(i)
Next i
ave = Sum / UBound(a)
End Function
5编写程序,计算并输出下面级数前n项(n=50)中偶数项的和。
s = 1*2+2*3+3*4+4*5+......+n*(n+1)+...
22750
Private Sub Form_Activate()
s = 0
For I = 2 To 50 Step 2
s = s + I * (I + 1)
Next I
Print s
End Sub
6编写程序, 设n=20,x=3.4,编写程序,计算并输出S(n)的值,要求结果保留5位小数。S(n)=lnx/x+ln2x/x^2+ln3x/x^3+...+ln(n*x)/x^n+...(其中,ln为自然对数函数)
Private Sub Command1_Click()
Const x = 3.4
For n = 1 To 20
s = s + Log(n * x) / x ^ n
Next n
Print Format(s, "0.>")
End Sub
7随机输出一个小写字母
Dim letterasscii, i%, letter$
letterascii = Int(Rnd * 26) + 97
letter = Chr(letterascii)
Print letter
MsgBox (letter)
如果是求大写字母就把97改为65
8编写一个函数,其功能为判断某个整数是不是素数并判断100001是否是素数。
Private Sub Form_Click()
Dim n, i#
n = InputBox("n=")
i = 2
While n Mod i <> 0
i = i + 1
Wend
If i = n Then
Print n & "是素数"
Else
Print n & "不是素数"
End If
End Sub
用VB编写程序,判断100到200之间的素数,并求和
Private Sub Command1_Click()
Dim i As Integer, j As Integer, sum As Integer
Print "100到200之间的素数:"
For i = 100 To 200
If Prime(i) Then
j = j + 1
Print CStr(i) & " ";
If j Mod 5 = 0 Then Print
sum = sum + i
End If
Next i
Print
Print "总和:"
Print sum
End Sub
Private Function Prime(n As Integer) As Boolean
If n <= 1 Then
Exit Function
ElseIf n = 2 Then
Prime = True: Exit Function
ElseIf n Mod 2 = 0 Then
Exit Function
End If
Dim i As Integer
For i = 3 To Sqr(n) Step 2
If n Mod i = 0 Then Exit Function
Next i
Prime = True
End Function
判断一个数是否为素数
Dim n, i#
n = InputBox("n=")
i = 2
While n Mod i <> 0
i = i + 1
Wend
If i = n Then
Print n & "是素数"
Else
Print n
& "不是素数"
End If
9编写程序实现,从键盘输入一个十六进制数,将其转换为十进制数,并输出结果。要求:转换过程用Function过程实现。
Private Sub Command1_Click()
a = InputBox("请输入十六进制数:", "输入")
Print a & "转成十进制:" & HEX_to_DEC(a)
End Sub
Public Function HEX_to_DEC(ByVal Hex As String) As Long
Dim i As Long
Dim B As Long
Hex = UCase(Hex)
For i = 1 To Len(Hex)
Select Case Mid(Hex, Len(Hex) - i + 1, 1)
Case "0": B = B + 16 ^ (i - 1) * 0
Case "1": B = B + 16 ^ (i - 1) * 1
Case "2": B = B + 16 ^ (i - 1) * 2
Case "3": B = B + 16 ^ (i - 1) * 3
Case "4": B = B + 16 ^ (i - 1) * 4
Case "5": B = B + 16 ^ (i - 1) * 5
Case "6": B = B + 16 ^ (i - 1) * 6
Case "7": B = B + 16 ^ (i - 1) * 7
Case "8": B = B + 16 ^ (i - 1) * 8
Case "9": B = B + 16 ^ (i - 1) * 9
Case "A": B = B + 16 ^ (i - 1) * 10
Case "B": B = B + 16 ^ (i - 1) * 11
Case "C": B = B + 16 ^ (i - 1) * 12
Case "D": B = B + 16 ^ (i - 1) * 13
Case "E": B = B + 16 ^ (i - 1) * 14
Case "F": B = B + 16 ^ (i - 1) * 15
End Select
Next i
HEX_to_DEC = B
End Function
编写一个函数,其功能是把一个二进制数串转换为十进制数串,并计算"1110011"对应的十进制数
Private Sub Command1_Click()
Print BIN_to_DEC(1110011)
End Sub
Public Function BIN_to_DEC(ByVal Bin As String) As Long
Dim i As Long
For i = 1 To Len(Bin)
BIN_to_DEC = BIN_to_DEC * 2 + Val(Mid(Bin, i, 1))
Next i
End Function
10编写一个函数,其功能为求两个数的最大公约数。
Private Sub Form_Click()
Dim m%, n%
m = InputBox("请输入第一个自然数")
n = InputBox("请输入第二个自然数")
Print m; "和"; n; "的最大公约数是";
Print gcd(m, n)
End Sub
Public Function gcd%(x%, y%)
Dim r%
r = x Mod y
While r <> 0
x = y
y = r
r = x Mod y
Wend
gcd = y
End Function
11输入整数M,N,编程让m>n,并输出m,n的值
Private Sub Command1_Click()
Dim m, n%
m = Val(InputBox("m"))
n = Val(InputBox("n"))
If m < n Then
k = m: m = n: n = k
End If
Print m; n;
End Sub
12计算并输出下面级数前n项(n=20)中奇数项的和1* +(-1)^(n-1)*n*(n+1)*(n+2)+
Private Sub Command1_Click()
Dim s!, i%
For i = 1 To 20 Step 2
s = s + i * (i + 1) * (i + 2)
Next
MsgBox (s)
End Sub答案 24090
编程 设n=20,编写程序,计算并输出 S(n)的值,保留5位小数
S(n)=(1*2)/(3*4)+(3*4)/(5*6)+(5*6)/(7*8)+......+((2n-1)*2n)/((2n+1)*(2n+2))+.......
Private Sub Command1_Click()
Dim i%, k#, sum#
i = 1
Do
k = ((2 * i - 1) * i * 2) / ((2 * i + 1) * (2 * i + 2))
sum = sum + k
i = i + 1
Loop While i <= 42
Print Format(sum, "0.00000")
End Sub
编程,计算并输出下面级数,当n=15,x=3.1416
时的部分和,结果取6位小数。
1+x+x^2/2!+x^3/3!+...+x^n/n!+...
Private Sub Command1_Click()
Dim i As Integer
Dim sum As Single
Dim a As Double
Dim b As Single
a = 1: x = 3.1416: sum = 1
For i = 1 To 15
a = a * i
b = x ^ i / a
sum = sum + b
Next i
Print Format(sum, "0.000000")
End Sub
13定义一个下标上界为10的整型数组,并用随机函数为其赋值(值介于[0,100]之间计算数组中所有偶数的数据的和:
Dim a%(10), i, sum%
For i = LBound(a) To UBound(a)
Randomize
a(i) = Rnd * 100
If a(i) Mod 2 = 0 Then
sum = sum + a(i)
End If
Next
Print sum
14判断输入年份是否闰年;
dim m%
if m mod 400=0 or m mod 100<>0 and m mod 4=0 then
print "YES"
else
print "NO"
15判断一个数是否为水仙花数
a = m Mod 10
b = (i - a) / 10 Mod 10
c = i \ 100
If a ^ 3 + b ^ 3 + c ^ 3 = i Then
MsgBox "yes"
Else
MsgBox "NO"
End If
出100到1000间的水仙花数
Private Sub Command1_Click()
Dim i%, a, b, c%
For i = 100 To 999
a = i Mod 10
b = (i - a) / 10 Mod 10
c = i \ 100
If a ^ 3 + b ^ 3 + c ^ 3 = i Then
Print i;
End If
Next i
End Sub
16计算sum=1!+3!+5!+...+9!
Private Sub Command1_Click()
Dim i%,j%,sum#,a#
sum = 0
For i = 1 To 9 Step 2
a=1
For j = 1 To i
a = a * j
sum = sum + a
Next j
Next i
Print sum
End Sub、
计算sum=2!+4!+···+10!
For i = 1 To 9 Step 2
换成For i = 0 To 9 Step 2
计算sum=1*3*··*9
Private Sub Command1_Click()
Dim sum As Long, i As Integer
sum = 1
For i = 1 To 9 Step 2
sum = sum * i
Next
text1.Text = sum
End Sub
17计算sum=1/1!+1/2!+```+1/10!
Private Sub Command1_Click()
Dim m As Integer, n As Long, s As Double
n = 1
s = o
For m = 1 To 10
n = n * m
s = s + 1 / n
Next
Print s
End Sub
18输出一位在(0~100)的整数
Private Sub Command1_Click()
Randomize
Dim s%
s = Int(101 * Rnd)
Print s
End Sub
19当x为偶数时y为x的2分之一
当x为奇数时y为x的2倍
Private Sub Command1_Click()
Dim x!, y!
x = Val(InputBox("请输入x的值"))
If x Mod 2 = 0 Then
y = x / 2
Else
y = 2 * x
End If
MsgBox (y)
End Sub
20计算1~100的5的倍数和7的倍数之和
Private Sub Form_Click()
Sum = 0
For i = 1 To 100
If i Mod 5 = 0 Or i Mod 7 = 0 Then
Sum = Sum + i
End If
Next i
Print Sum
End Sub
编写程序,求下面级数前n项和s=1/1!-1/2!+1/3!-1/4!~~~~(-1)^(n-1)1/n!求和过程当某一项的绝对值小于0.0001时结束(该项参与求和),计算并输出所求的和s,要求结果保留6位小数
Private sub command1-click()
Dim m as double,n as long,s as double,,f as integer
S=0:n=-1:m=1:f=1
Do while 1/m>=0.0001
M=m*f
F=f+1
N=-n
S=s+n/m
Loop
S=format(s,"0.000000")
Print s
End sub
1、》输入长方形的长和宽,计算长方形的面积,并输出。
Private Sub command1_click()
Dim a%, b%, s%
a = Val(InputBox("a"))
b = Val(InputBox("b"))
s = a * b
Print s
End Sub
2、 输入正方形的边长,计算正方形的对
角线长,并输出。
Private Sub command1_click()
Dim a!,l!
a=val(inputbox("输入边长"))
l=sqr(2)*a
Print "对角线长度为:";l
End sub
3、输入圆的半径,计算圆的周长,并输出。
Private sub command1_click()
Const pi=3.14
Dim s#,i%
r=val(inputbox("请输入半径"))
s=2*pi*r
print "圆的周长为:";s
end sub
4、 》计算sum=1/2!+1/4!+1/6!+...+1/10!
Private sub command1_click()
Dim i%,f#,s#
F=1:s=0
For i=1 to 10
F=f*i
If i mod 2=0 then
S=s+1/f
End if
Next i
Print "sum=1/2!+1/4!+1/6!+...+1/10!";"=";s
End sub
5、 计算sum=2!+4!+6!+...+10! 《计算、sum=1!+3!+5!+...+9!》
Private sub command1_click()
Dim i%,f#,s#
F=1:s=0
For i=1 to 10
F=f*i
If i mod 2=0 then
S=s+f
End if
Next i
Print "sum=2!+4!+6!+...+10!";"="s
End sub
6、计算sum=2+4+6+...+100
Private Sub Command1_Click()
Dim i%, sum%
sum = 0
For i = 2 To 100 Step 2
sum = sum + i
Next i
Text1.Text = sum
End Sub
7、计算sum=1+3+5+...+99
Private Sub command1_click()
Dim i%, sum%
For i = 1 To 99 Step 2
sum = sum + i
Next
Print sum
End Sub
8、 编写程序,建立一个 6×6的整型二维数组(数据由程序调用Rnd函数产生[0,15 )的随机整数数),输出数组中的最小元素。
Private Sub command1_click()
Dim a%(6, 6), i%, j%
For i = 1 To 6
For j = 1 To 6
a(i, j) = Int(Rnd * 15)
Print a(i, j);
Next j
Print
Next i
Min = a(1, 1)
For i = 1 To 6
For j = 1 To 6
If Min > a(i, j) Then Min = a(i, j)
Next j
Next i
Print "数组中最小元素为:"; Min
End Sub
11、随机输出一个位于[0,100]的整数。
Private Sub Command1_Click()
Dim s#
S=int(rnd *101) '括号里是r n d ,不是md
Print "随机整数为:";s
End sub
12、 编写一个函数,其功能为求两个数的最小数。
Private Sub Command1_Click()
Dim x!, y!
x = InputBox("输入第一个数值 ")
y = InputBox("输入第二个数值 ")
Min = f(x, y)
End Sub
Public Function f(m!, n!)
Min = m
If Min > n Then Min = n
f = Min
Print "两数中的较小数是"; Min
End Function
16、 编写一个函数,其功能为判断某个整数是不是素数。调用该函数,计算并输出区间 [1300 , 1400] 内所有素数的和。 ( 提示:只能被 1 和自身整除的自然数称为素数。 )
Private Sub Command1_Click()
Dim i%
s = 0
For i = 1300 To 1400
If f(i) Then s = s + i
Next i
Text1.Text = s
End Sub
Public Function f(n%) As Boolean
Dim i%
For i = 2 To n - 1
If n Mod i = 0 Then Exit For
Next i
If i > n - 1 And n > 1 Then
f = True
Print n; "是素数"
Else
Print n; "不是素数"
End If
End Function
17、 随机输出一个小(大)写字母。
Private Sub Command1_Click()
Dim s
s = Chr$(Int(Rnd * 26 + 97))
p = Chr$(Int(Rnd * 26 + 65))
Print "输出的随机小写字母是"; s
Print "输出的随机大写字母是"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
计算机等级考试二级VB基础知识
« 上一篇
VB考试复习资料(判断与填空)
下一篇 »
发表评论