急急急VB自定義計算階乘的函式過程fac n ,呼叫該函式過程計算s 1 2 3 4 5n

時間 2021-05-04 08:54:40

1樓:劍小風

看了上面幾位的** :應該都寫錯了

樓主的問題是:s等於 1到n之間每個整數的階乘相加,而不是n的階乘我沒理解錯吧?

稍等 完畢:

在模組中輸入**:

public function fac(n as integer) as long '在模組中用public(公用的)定義函式過程

dim i as integer, j as integer, s as long

for i = 1 to n

s = 1

for j = 1 to i

s = s * j

next j

fac = fac + s

next i

end function

在窗體中輸入**:

private sub form_click()dim s as long

dim n as integer

n = inputbox("請輸入一個整數")s = fac(n)

print s

end sub

2樓:

雙擊模組,選插入選單裡面的插入過程,選插入函式,命名為fac**如下:

public function fac(n as integer)dim i as integer, s as integerfor i = 1 to n

s = s + i

next

fac = s

end function

3樓:匿名使用者

private sub command1_click() 過程方法dim i as integer

for i = 1 to 10

sum = sum + jc(i)

next

msgbox sum

end sub

function jc(a as integer) 函式方法jc = 1

for i = 1 to a

jc = jc * i

next

end function請參考

4樓:匿名使用者

private function fac(byval n as integer) as single

dim i as integer

dim j as single

j = 1

for i = 1 to n

j = j * i

next i

fac = j

end function

vb程式設計"編寫一個求n!的階乘函式f(n),利用該函式計算二項式係數cmn=m!/(n!*(m-n)!

5樓:vb愛好者

列如bai

:dim a, b as integer

dim s as long

b = val(text1)

s = 1

for a = 1 to n

s = s * n

label1.caption = "你輸出du的" + str(n) + "的結果為" + str(s)

這只是zhi一個dao列子,你可內以開動容腦筋

6樓:匿名使用者

private sub command1_click()dim m as integer, n as integerm = int(inputbox("輸入第一個整數專"))n = int(inputbox("輸入第一個整數"))

if m >= n then

cmn = jiecheng(m) / (jiecheng(n) * jiecheng(m - n))

print cmn

else: msgbox "輸入資料有錯

屬"end if

end sub

private function jiecheng(x as integer) as double

jiecheng = 1

for i = 1 to x

jiecheng = jiecheng * inext

end function

用vb編寫程式,求s=a!+b!+c!,階乘的計算分別用sub過程和function過程兩種方法來實現

7樓:匿名使用者

(1)用sub實現的過程bai

如下:du

dim a,b,c,s

a=1b=2

c=3s=0

sub jc(x)

dim v

v=1while x>0

v=v*x

x=x-1

wend

s=s+v

end sub

jc a

jc b

jc c

msgbox s

(2)用function的過程zhi如下

function

function jc(a)

if a>0 then

jc=a*jc(a-1)

else

jc=1

end if

end function

dim s,a,b,c

a=4b=7

c=5s=jc(a)+jc(b)+jc(c)

8樓:網海1書生

:private sub command1_click()

dim a as long, b as long, c as long

a = val(inputbox("請輸入一個大於0的整數a:"))

b = val(inputbox("請輸入一個大於0的整數b:"))

c = val(inputbox("請輸入一個大於0的整數c:"))

if a > 0 and b > 0 and c > 0 then

n1 a

n1 b

n1 c

msgbox "a!+b!+c!=" & a + b + c

end if

end sub

sub n1(n as long)

dim i as long, s as long

s = 1

for i = 2 to n

s = s * i

next

n = s

end sub

function實現:

private sub command1_click()

dim a as long, b as long, c as long

a = val(inputbox("請輸入一個大於0的整數a:"))

b = val(inputbox("請輸入一個大於0的整數b:"))

c = val(inputbox("請輸入一個大於0的整數c:"))

if a > 0 and b > 0 and c > 0 then

msgbox "a!+b!+c!=" & n2(a) + n2(b) + n2(c)

end if

end sub

function n2(n as long) as long

dim i as long

n2 = 1

for i = 2 to n

n2 = n2 * i

next

end function

請注意:輸入的數字不能太大,因為受long型最大值的限制,計算13!即溢位。要想計算更大的數值,可使用double數值型別

求vb題**:求任意個數的階乘累計和。s=1!+2!+3!+...+n!,n由使用者輸入。

9樓:烽火魂

private sub command1_click()

dim i as double

dim n as double

dim sum as double

sum = 0

if not isnumeric(text1.text) then  '判斷是否為數字,不是數字就彈出對話方塊並對出過程

call msg    '呼叫已經寫好的過程

exit sub

end if

n = text1.text

if n < 1 then  '判斷是否小於1 ,如果小於1就呼叫已經定義好的過程彈出對話方塊

call msg

exit sub

end if

for n = 1 to n    '求1!到n!階乘迴圈n次

sum = sum + func(n)   '呼叫求第n數的的階乘的函式,計算前1!+n!的和

next n

msgbox "1!+2!+3!+......+n!為" & sum, vbinformation, "結果"

end sub

'求第n個數的階乘函式

function func(n as double) as double

dim i as double

dim jc as double

jc = 1

for i = 1 to n

jc = jc * i

next i

func = jc

end function

'彈出對話方塊

sub msg()

msgbox "你輸入資料不正確", vbinformation, "提示"

end sub

10樓:

一樓的很複雜,給你來個簡單的:

private sub commandbutton1_click()dim i, s, k

dim n

aaa: n = inputbox("請輸入大於等於1的數字")if val(n) >= 1 then

if len(n) = 0 then endfor i = 1 to n

s = s + i * i

next

msgbox "從1到" & n & "階乘累計和為:" & selse

k = msgbox("你輸入資料不符合標準,請重輸入:", vbokcancel, vbinformation, "提示")

if k = vbok then

goto aaa

else

endend if

end if

end sub

vb題目急急急

dim a as string,b as stringprivate sub command1 click a text1.text b text2.text end sub private sub command2 click text3.text a b 連線順序 a前b後,可改b a b前a後...

vb程式設計急急急急急急急急急急急急急

我有類似現成的程式,是我給孩子編的,你給個郵箱,給你發過去. 下面的 另存為form1.frm version 5.00 begin vb.form form1 caption form1 clientheight 8985 clientleft 60 clienttop 345 clientwid...

有關VB6 for next語句的問題急急急急急急急急急急好的加分

常山大寶 下面是command1單擊事件 private sub command1 click on error goto err dim a as integer a me.text1 me.label4.caption 0for i 1 to text2 me.label4.caption me...