tfsf.net
当前位置:首页>>关于在vb中输入一个数n,计算n的阶乘,将结果通过标签控...的资料>>

在vb中输入一个数n,计算n的阶乘,将结果通过标签控...

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

Dim n As Long n = InputBox("整数") Dim i As Long Dim j As Long Dim s As String Dim x As Double Print "0!=1" For i = 1 To n x = 1 For j = 1 To i x = x * j Next Print j - 1 & "!=" & x Next

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)

#include <stdio.h>int main(){ int n,sum=1; scanf("%d",&n); for(int i=2;i<=n;i++) sum*=i; printf("%d\n",sum); return 0;}

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

思路:所谓n的阶乘就是从1到n的累积,所以可以通过一个for循环,从1到n依次求积即可.参考代码:#include <stdio.h> int main() { int n,i,s=1; scanf("%d",&n); for(i=1;i<=n;i++)//for循环求累积 s=s*i; printf("%d\n",s); return 0; }/* 运行结果:(例如求5的阶乘) 5120*/

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)

function jc(n as integer) as longdim i as integerjc=1for i=2 to n jc=jc*inextend functionPrivate sub form_click()dim n as integer, s as longdo n=n+1 s=jc(n)loop until s>10^6print "当n大于等于"; n; "时1*2*3**n的值大于10^6"end sub

用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的阶乘

'调用示例 Private Sub Form_Load() MsgBox fun_JC(3) End Sub'阶乘函数 Private Function fun_JC(ByVal N As Integer) As Long Dim i As Integer Dim l As Long l = 1 If N fun_JC = 1 Exit Function End If For i = N To 2 Step -1 l = l * i Next fun_JC = l End Function

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