VB求1到N阶乘

编写程序求S=1!+2!+3!+···+n!,n的值由键盘输入,n<13

Option Explicit

Private Sub Form_Load()
    Dim n As Integer, i As Integer, L As Long
    Me.AutoRedraw = True
    
    n = InputBox("输入n(n<13):")
    
    If n >= 13 Then Exit Sub
    
    
    L = 1
    For i = 1 To n Step 1
          L = L * i
    Next i
    
    Print n & "! = " & L
    
    
End Sub

温馨提示:内容为网友见解,仅供参考
第1个回答  2010-03-29
不用任何控件,把下面的代码复制到代码窗口中,运行时单击窗口即可:

Private Sub Form_Click()
Dim S As Double, N As Integer, I As Byte
Dim St As String
Form1.AutoRedraw = True
Do
St = InputBox("N的值(N<13)", "输入", 5)
If St <> "" Then
N = Int(Val(St))
If N >= 1 And N < 13 Then Exit Do
End If
Loop
For I = 1 To N
S = S + Jc(I)
Next
Print "S("; Trim(N); ")= 1!";
For I = 2 To N
If I = 4 And N <> 4 Then
Print "+......";
ElseIf I > 4 And I <> N Then
Print "";
Else
Print "+"; I; "!";
End If
Next
Print "="; S
End Sub

Private Function Jc(X As Byte) As Double
If X = 1 Then Jc = 1 Else Jc = X * Jc(X - 1)
End Function

已经运行过。
第2个回答  2010-03-29
Private Sub Command1_Click()
Dim aa%, bb%, cc%
bb = 1
cc = 0
aa = Val(InputBox("请输入一个小于13的整数"))
For i = 1 To aa
bb = bb * i
cc = cc + bb
Next i
MsgBox cc
End Sub

简单无控件本回答被提问者采纳
第3个回答  2010-03-29
Private Sub Command1_Click()
Dim i As Integer, j As Integer, q As Long
i = Val(Text1.Text)
For j = 1 To i
q = q + ss(j)
Next j
Text2.Text = q
End Sub

Function ss(n As Integer) As Long
If n = 0 Then
ss = 1
Else
ss = n * ss(n - 1)
End If
End Function

回答完毕,期待被采纳!

在VB中求阶乘的代码
在VB中求阶乘的部分代码:Private Function fact(m As Integer) As Long Dim i As Integer fact = 1 For i = 1 To m fact = fact * i Next i End Function Private Sub Command1_Click()Dim n As Integer, sum As Long Dim i As Integer n = Val(Text1.Text)For i = 1 To ...

怎么写vb的阶乘代码
1、For语句实现 Private Sub Command1_Click()Dim s As Long, n As Integer, i As Integer n = Val(Text1.Text)s = 1 For i = 1 To n s = s * i Next i Label4.Caption = Str(s)End Sub 2、Do While语句实现 Private Sub Command1_Click()Dim s As Long, n As Integer, ...

如何用VB语言编程求数学阶乘?
1、首先定义一个函数,这个函数里的内容求n的阶乘,返回值是n的阶乘,如下图所示。2、函数定义完成之后,就要开始写main函数里的内容,还是定义变量。3、变量定义后就要赋值,把sum的值写为0.因为后面的求和就需要sum的初始值为0,如下图所示。4、下一步就是利用for循环了,for循环i初始值位1,结束...

VB求1到N的阶乘
MsgBox ("请重新输入小于13的数据") Else Exit Do End IfEnddoS = 0For i = 1 To n Si = 1 For j = 2 To i Si = Si * j Next S = S + SiNext Print "S = 1!+2!+3!+···+n! = "; Format(S)...

编写VB阶乘的函数
在VB编程中,要创建一个计算阶乘的函数,你可以参考以下步骤:首先,定义一个名为jiecheng的Public函数,它接受一个整数n作为参数,返回计算结果。函数的实现是通过一个简单的循环,初始化jc为1,然后从1到n逐个乘以jc,最后返回jc的值。代码如下:Public Function jiecheng(ByVal n As Integer) As ...

...一的阶乘加二的阶乘加三的阶乘一直加到N的阶乘
Private Sub Command1_Click()n = Val(InputBox("n="))t = 1 For i = 1 To n t = t * i s = s + t Next i Print "1!+2!+...+"; n & "!="; s End Sub

VB阶乘的算法
求阶乘的两种算法 方法一(计数循环)使用For语句。Function Factorial(n As Integer) As Long On Error GoTo Hander Dim i As Long Factorial = 1 For i = n To 1 Step -1 Factorial = Factorial * i Next i Exit Function Hander:MsgBox "数值过大!", vbExclamation, "Error"Resume Next ...

vb系统如何求阶乘
Function jc(n as integer) as long dim i as integer ,m as long m=1 for i=1 to n m=m*i next jc=m End Function

用VB编程算1*2*3*...*n
Private Sub Command1_Click() Dim s As Double, n As Integer, i As Integer s = 1 n = InputBox("请输入数据") For i = 1 To n s = s * i Next i Print sEnd Sub

VB编程输入任意一个正整数N如果N为偶数,求N的阶乘如果N为奇数,求1...
For i As Integer = 2 To N Step 2 factorial *= i Next Console.WriteLine("N 的阶乘为:" & factorial)Else ' 如果 N 是奇数,计算 1 到 N 的累加和 Dim sum As Long = 0 For i As Integer = 1 To N sum += i Next Console.WriteLine("1 到 N 的累加和为:" & sum)End ...

相似回答