累加求和1到100Private Sub Command1_Click()Dim i As Integer, sum As Integersum = 0For i = 1 To 100sum = sum + iNextText1.Text = sumEnd SubPrivate Sub Command2_Click()EndEnd Sub1到100的奇数和Private Sub Command1_Click()Dim i As Integer, sum As Integersum = 0For i = 1 To 100 step 2sum = sum + iNextText1.Text = sumEnd SubPrivate Sub Command2_Click()End求n!Private Sub Command1_Click()Dim fact As Double, n As Integer, i As Integer n = Val(Text1.Text)fact = 1For i = 1 To nfact = fact * iNextText2.Text = factEnd Sub累加求和1到100(Do while)Private Sub Command1_Click()Dim i As Integer, sum As Integersum = 0i = 1Do While i <= 100sum = sum + ii = i + 1LoopText1.Text = sumEnd SubPrivate Sub Command2_Click()EndEnd Sub(Do Until)Private Sub Command1_Click()Dim i As Integer, sum As Integersum = 0i = 1Do until i >100sum = sum + ii = i + 1LoopText1.Text = sumEnd SubPrivate Sub Command2_Click()EndEnd Sub求平均成绩Private Sub Command1_Click()Dim a As Single, b As Single, c As Single, d As Single Dim pj As Singlea = 60:b = 70:c = 80:d = 90pj = (a + b + c + d) / 4Text1.Text = Str(pj)End Sub求圆的周长Private Sub Command1_Click()Dim r As DoubleConst pi = 3.1415926r = Val(InputBox("请输入圆的半径"))Print "圆的周长为:", 2 * pi * rEnd Sub输入两个整数m n,使得m>=n,并输出m nPrivate Sub Command1_Click()Dim m As Integer, n As Integer, a As Integerm = Val(InputBox("请输入m:", 输入, "m"))n = Val(InputBox("请输入n:", 输入, "n"))If n > m Thena = mm = nn = aEnd IfPrint "m=", mPrint "n=", nEnd Sub计算sum=1/1!+1/2!......+1/10!Private Sub Command1_Click()Dim m As Integer, n As Long, s As Doublen = 1s = 0For m = 1 To 10n = n * ms = s + 1 / nNextPrint sEnd Sub定义一个下标上界为10的整型数组,并随机函数为其赋值(值处于【0,100】之间,计算数组中所有偶数的和Private Sub Command1_Click()RandomizeDim a%(10)Dim i%, s%For i = 0 To 10a(i) = 101 * Rnd()NextFor i = 0 To 10If a(i) Mod 2 = 0 Thens = s + a(i)End IfNextPrint "所有偶数的和为:", sEnd Sub输入一个四位数,分别输出它的个位数字、十位数字、百位数字、和千位数字。
(待修改)Private Sub Command1_Click()Dim a%, i%, k%, m%, s$a = Val(InputBox("请输入一个四位数"))i = a Mod 10j = (a Mod 100) \ 10k = (a Mod 1000) \ 100m = a \ 1000s="个位数字是:"&i&vbcrlf"十位数字是:"&j&vbcrlf&"百位数字是:"&k&vbcrlf&"千位数字是:"&mPrint sEnd Sub计算sum=2!+4!+6!+ (10)Private Sub Command1_Click()Dim a!, i%, sum!i = 1: a = 1Doa = 2 * iIf i Mod 2 = 0 Thensum = sum + aEnd Ifi = i + 1Loop While i <= 10Print sumEnd Sub用inputbox输入一个年份,判断该年份是否是闰年,若是则输出Yes,否则输出NoPrivate Sub Command1_Click()Dim j%j = Val(InputBox("请输入年份"))If j Mod 4 = 0 ThenMsgBox ("Yes")ElseMsgBox ("No")End IfEnd Sub计算并输出下面级数前n项(n=20)中奇数项的和。
1*2*3-2*3*4+3*4*5-4*5*6+........+(-1)……(n-1)*n*(n+1)*(n+2)+.....Private Sub Command1_Click()Dim s!, i%For i = 1 To 20 Step 2s = s + i * (i + 1) * (i + 2)NextMsgBox (s)End Sub如果一个三位数等于其各位数字的立方和,则称其为水仙花数。
请输入一个三位数,并判断其是否为水仙花数。
若是,用MsgBox输出“yes”,否则输出“no”Private Sub Command1_Click()Dim a%, i%, j%, k%a = InputBox("请输入一个三位数!")i = a \ 100j = a \ 10 Mod 10k = a Mod 10If a = i ^ 3 + j ^ 3 + k ^ 3 ThenMsgBox ("yes")ElseMsgBox ("no")End IfEnd Sub某分段函数描述如下:(1)当x为偶数时,y的值为x的二分之一(2)当x为奇数时,y的值为x的二倍Private Sub Command1_Click()Dim x!, y!x = Val(InputBox("请输入X的值!"))If x Mod 2 = 0 Theny = x / 2y = 2 * xEnd IfMsgBox (y)End Sub输入三个数,判断其是否能够构成三角形...........Private Sub Command1_Click()Dim i As Integer, a(2) As Integer, p As Single, s As SingleFor i = 0 To 20a(i)=val(inputbox("输入第"&i&"条边的边长:"))Next iIf a(0) + a(1) > a(2) And a(0) + a(2) > a(1) And a(1) + a(2) > a(0) Thenp = (a(0) + a(1) + a(2)) / 2s=sqr(p*(p-a(0)*(p-a(1))*(p-a(2)))MsgBox (s)ElseMsgBox ("不能形成三角形!")End IfEnd Sub计算1*3*5....*9Private Sub Command1_Click()Dim sum As Long, i As Integersum = 1For i = 1 To 9 Step 2sum = sum * iNextMsgBox (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 Integer, s As Double, f As Integers = 0: n = -1: m = 1: f = 1Do While 1 / m >= 0.0001m = m * ff = f + 1s = s + n / mLoops = Format(s, "0.000000")Print sEnd Sub随即输出一个位于【0,100】之间的整数Private Sub Command1_Click()RandomizeDim s%s = (101 * Rnd)Print sEnd Sub编写程序,计算并输出下面级数前n项(n=50)中的偶数项的和Private Sub Command1_Click()Dim s!, i%For i = 2 To 50 Step 2s = s + i * (i + 1)NextMsgBox (s)End Sub《VB期末范围题总汇》1.求随机10个整数的最大值、最小值、平均值以及和;Dim a(1 To 10)Private Sub Command1_Click()RandomizePicture1.Print "产生的随机数为:"For i = 1 To 10a(i) = Int(Rnd * 99 + 1)Picture1.Print a(i);Next iPicture1.PrintEnd SubPrivate Sub Command2_Click()Dim max, min, avemax = a(1)min = a(1)ave = 0.1 * a(1)For i = 2 To 10If a(i) > max Then max = a(i)If a(i) < min Then min = a(i)ave = ave + 0.1 * a(i)Next iPicture1.Print "最大数为:"; maxPicture1.Print "最小数为:"; minPicture1.Print "平均数为:"; aveEnd Sub2.求水仙花数Private Sub Form_Click()Dim a, b, c As Integer 'a(个)b(十)c(百)For a = 0 To 9For b = 0 To 9For c = 1 To 9If a ^ 3 + b ^ 3 + c ^ 3 = a + 10 * b + 100 * c Then MsgBox 100 * c + 10 * b + aEnd IfNext cNext bNext aEnd Sub求1000以内的所有完数一个按钮里调用的过程Private Sub Command1_Click()OutputWanNumber 1000End Sub5.求各位数字之和input a:'输入任意数dob=a mod 10:'取a的末位数sum=sum+b:'求和a=a \ 10:'去掉末位数loop until a=0print "sum=";sum:'输出end(dim n as stringinput nfor i = 1 to len(n)sum=sum+val(mid(n,i,1))next iprint sum)6.求最小公倍数Private Sub Form_Load()Form1.AutoRedraw = TrueDim n1%, m1%, m%, n%, r%n1 = InputBox("输入n1")m1 = InputBox("输入m1")If m1 > n1 Then '为了求最小公倍数,增加m,n变量m = m1: n = n1Elsem = n1: n = m1End IfDor = m Mod nIf r = 0 Then Exit Dom = nn = rLoopPrint n1; ","; m1; "的最大公约数为"; nPrint "最小公倍数=", m1 * n1 / nEnd Sub7.求逆序数(感觉题目类型太多)9. 求质因子问题Private Sub Command1_Click()Dim N As Integer, I As IntegerN = Val(InputBox("请输入2的整数:"))I = 2DoIf N Mod I = 0 ThenPrint I;N = N \ IElseI = I 1End IfLoop While N 1End Sub1. 判定素数过程Function isprime(Num As Long) As BooleanIf Num < 2 Then isprime = False: Exit Function Dim i As LongFor i = 2 To Sqr(Num)If (Num Mod i) = 0 Thenisprime = FalseExit FunctionEnd IfNext iisprime = TrueEnd FunctionPrivate Sub Command1_Click()Dim i As LongFor i = 1 To 1000If isprime(i) ThenPrint iEnd IfNext iEnd Sub2.求最大公约数过程;Function Max公约数(A As Long, B As Long) '求出两个数的最大公约数Dim X As Long, Y As Long, K As LongX = IIf(A >= B, A, B) 'x存入最大值Y = IIf(A <= B, A, B) 'y 存入最小值Do '辗转相除法K = X Mod Y: If K = 0 Then Exit DoX = Y: Y = KLoopMax公约数 = YEnd Function3.冒泡排序过程Private Sub Command1_Click()Dim a(9) As IntegerDim i As IntegerFor i = 0 To 9a(i) = InputBox("输入整数")NextSort aFor i = 0 To 9Print a(i)NextEnd SubPrivate Sub Sort(ByRef a() As Integer)Dim i As IntegerDim j As IntegerDim t As IntegerFor i = LBound(a) + 1 To UBound(a)For j = UBound(a) To i Step -1If a(j - 1) > a(j) Thent = a(j - 1)a(j - 1) = a(j)a(j) = tEnd IfNextNextEnd Sub4顺序查找过程Private Sub Command1_Click()Dim i, j, t, a(1 To 10)RandomizePrint "原数组:"For i = 1 To 10a(i) = Rnd * 10Print "a(" & i & ") =" & a(i) & Space(2),If i Mod 2 = 0 Then PrintNext iPrintFor i = 1 To 9For j = i + 1 To 10If a(j) < a(i) Thent = a(i)a(i编程题(20分,正式试题为1题)1、编写一个身份证号码转换程序:(1) 老身份证是15位,先要增加两位年份,变成17位,再计算校验位。