tfsf.net
当前位置:首页>>关于VB求N的阶乘怎么做啊?快(详细的)的资料>>

VB求N的阶乘怎么做啊?快(详细的)

递归法,下面这个是个编写出来的阶乘函数,直接调用就可以了 Private Function jiecheng(n As Integer) As Integer If n = 1 Then jiecheng = 1 Else jiecheng = n * jiecheng(n - 1) End If End Function

private sub command1_click() sum = 1 for i = 1 to text1.text sum = sum * i next i print sum end sub

你可以把它写成一个函数,便于调用,如下:Function Factorial(ByVal i As Integer) As Long Dim s As Long s = 1 If i > 0 Then Do While i > 0 s = s * i i = i - 1 Loop Factorial = s Else Factorial = 0 End If End Sub调用方法如下:num = Factorial(3) ' num = 6肯定对哦,求采纳么么哒 o(∩_∩)o

dim s as double, n as integern = val(inputbox("请输入n值,不要太大哦,否则出错"))s = 1do while n > 1 s = s * n n = n - 1loopmsgbox "这个数的阶乘为" & s

用vb求1到n的阶乘和的程序:1. 自己编写函数代码:Private Function jieC(Byval n As Integer) As Long Dim i As Integer Dim Fac As Long Fac = 1 For i = 1 To n Fac = Fac * i Next i jieC = Fac End Function2. 在需要求阶乘的地方,调用该函数:例如:Dim y As Long y = jieC(10) '求阶乘(10)将用10调用上面的自定义函数,返回10的阶乘

dim n, i, j as integer, s, si as long do while .t. n = inputbox("请输入小于13的数", "n=", 0) if n >= 13 then msgbox ("请重新输入小于13的数据") else exit do end if enddo s = 0 for i = 1 to n si = 1 for j = 2 to i si = si * j next s = s + si nextprint "s = 1!+2!+3!++n! = "; format(s)

Private Sub Command1_Click() Dim n As Integer n = InputBox("请输入一个正整数: ") Print Fac(n) End Sub Private Function Fac(m) As Long '**********found*************' '请不要删除该行,只在下一行中填空' Fac = 1 '**********found*************' '请不要删除该行,只在下一行中填空' For k = 2 To m Fac = Fac * k Next k End Function

作一个阶乘的函数:function factor(byval n as long) as long s = 1; for i = 1 to n s = s * i next i factor=send function调用此函数:(例:求100的阶乘,当然,你也可以是其他值.)private sub command1_click() print factor(100)end sub

Private Sub Command1_Click() n = CInt(Text1) m = CInt(Text2) f = 1 If m > n - m Then m = n - m For i = 1 To m f = f * (n + 1 - i) / i Next i Text3 = f End Sub

Private Sub Command1_Click() Dim fac As Double, n As Integer, i As Integer fac = 1 n = CInt(InputBox("请输入要计算阶乘的数N:")) If n < 0 Then MsgBox "N不能小于0!": Exit Sub For i = 2 To n fac = fac * i Next i MsgBox n & "!=" & fac End Sub

网站首页 | 网站地图
All rights reserved Powered by www.tfsf.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com