用vb编写一个程序,求一个整数的阶乘。急用谢谢

用vb编写一个程序,求一个整数的阶乘。急用谢谢,第1张

在窗口添加一个按钮

Private

Sub

Command1_Click()

Dim

i

As

Integer

Dim

j

As

Integer

Dim

s

As

Single

s

=

1

i

=

Val(InputBox("请输入需要求的阶乘"))

For

j

=

1

To

i

s

=

s

j

Next

j

Print

i

&

"阶乘是"

&

s

End

Sub

Function c(ByVal m As Integer) As Long

If m = 0 Then

c = 1 ' Return 1 不是用这个返回, 直接赋值

Else

c = m c(m - 1) 'Return m c(m - 1) '同上

End If

End Function

Private Sub Button1_Click(ByVal sender As SystemObject, ByVal e As SystemEventArgs) Handles Button1Click

Dim N As Integer , M As Integer , a As Integer, b As Integer

Dim x1 as long , x2 as long , x3 as long

N =val (TextBox1Text) 'N = TextBox1Text

M = val(TextBox2Text ) 'M = TextBox2Text

If N > M Then

a = N - M

x1 = c(N)

x2 = c(M)

x3 = c(a)

b = x1 / (x2 x3)

TextBox3Text = b

End If

End Sub

Private Sub Form_Click()

Dim n As Integer, t#, i%

n = InputBox("输入正整数", "阶乘")

t = 1

i = 1

Do While i <= n

t = t i

i = i + 1

Loop

Print n & "!=" & t

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

Private Sub Command1_Click()

Dim i, b As Integer

Dim a As Double

a = 1

For i = 1 To b

a = i a

Next i

'a为b的阶乘

Text1Text = a

End Sub

Public Function Fac(ByVal n As integer) As Long

Dim i As Integer

For i=1 to n

Fac=Faci

Next i

End Function

在窗体上放一个命令按纽,计算结果将在窗体上输出,代码如下(直接拷贝)

Private Sub Command1_Click()

Dim a As Integer, b As Integer, c As Integer

a = 2

b = 4

c = 6

nn a, b, c

End Sub

Sub nn(a As Integer, b As Integer, c As Integer)

Dim i As Integer, j As Integer, k As Integer, s1 As Integer, s2 As Integer, s3 As Integer

s1 = 1

s2 = 1

s3 = 1

For i = 1 To a

s1 = s1 i

Next i

For j = 1 To b

s2 = s2 j

Next j

For k = 1 To c

s3 = s3 k

Next k

Print "2!+4!+6!=" & s1 + s2 + s3

End Sub

Private Sub Command1_Click()

Dim A, B, C

A = 10

B = 3

C = f(A) / (f(B) f(A - B))

Print C

End Sub

Private Function f(n)

Dim I As Integer

f = 1

For I = 1 To n

f = f I

Next I

End Function

以上就是关于用vb编写一个程序,求一个整数的阶乘。急用谢谢全部的内容,包括:用vb编写一个程序,求一个整数的阶乘。急用谢谢、用VB编一求阶乘的函数f(n),主调程序求组合数的程序,分别调用f(n),用来计算组合数的值、VB求阶乘的代码。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9740738.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存