野生藏獒捕猎视频:帮忙啊VB题目

来源:百度文库 编辑:神马品牌网 时间:2024/04/26 20:45:47
1)编写一程序,由用户输入一个值,求得它的阶乘,并打印出来。(即求n!)
2)编写一程序,由用户输入一个值,求从1开始到它本身各值相加的和,并打印出来.(即1+2+3+````+n)

1:
Module Module1

Sub Main()
Dim intN As Integer
Dim intR As Double
Dim n As Integer

Console.WriteLine("Input a number !")
intN = Val(Console.ReadLine())
intR = 1
For n = 1 To intN
intR = intR * n
Next
Console.WriteLine("The result is " & intR)
Console.ReadLine()
End Sub

End Module
输入数太大会算不出来的。

2:
Module Module1

Sub Main()
Dim intN As Integer
Dim intR As Double
Dim n As Integer

Console.WriteLine("Input a number !")
intN = Val(Console.ReadLine())
intR = 1
For n = 1 To intN
intR = intR + n
Next
Console.WriteLine("The result is " & intR)
Console.ReadLine()
End Sub

End Module

以上两段代码都调试过了。

Dim a As Integer
Dim b As Integer
b=0
For a = 1 To 10
b = b + a
Next
Print (b)

//乘
Dim a As Integer
Dim b As long //长整 否则溢出
b=1
For a = 1 To 10
b = b * a
Next
Print (b)