trying to create a simple control

  • Thread starter Thread starter Raphael Gluck
  • Start date Start date
R

Raphael Gluck

Hi
I'm really new to programming and i'm reading through a book, teach yourself
asp.net by SAMS.
I'm having some difficulty with one of the vb.net lessons.
What i am supposed to be doing is create simple function to return the
factorial amount of the numbers 1 to 5.
Factorial is where you take a number and multiply it by all the digits that
exist below that number, e.g 3 would be 3 * 2 * 1 which would give you a
total of 6.

I've been really stuck with this for hours on end. It doesnt help that there
are several very annoying mistakes in the book, ( a bad worker blames his
tools)
So i just wanted to post the code here,
Please somone show me where i am wrong.

Function Factorial (n as integer) as integer
Dim i as integer
for i = 1 to 5
i = i * (i-1)
next i
Return Factorial

End function

Sub Page_load (sender as object, e as EventArgs)
response.write(factorial)
end sub



Please help me.

Raphael
 
Your always getting zero aren't you?

try

Dim i As Integer

Dim fact As Double

fact = 5

For i = fact To 1 Step -1

fact = fact * i

Next

MsgBox(fact)



you use 'i' too many times, just changng the variable all over the place...
this should help you out.



-peace,

cJ
 
Peace to you too, (we could do with some of that around here).

I am actually getting an error
BC30455: Argument not specified for parameter 'n' of 'Public Function
Factorial(n As Integer) As Integer'.


This is my original code

Function Factorial (n as integer) as integer
Dim i as integer
for i = 1 to 5 i = i * (i-1)
next i
Return Factorial
End function
Sub Page_load (sender as object, e as EventArgs) response.write(factorial)
end sub
 
"Raphael Gluck".
I shall try to explain you
Factorial is where you take a number and multiply it by all the digits that
exist below that number, e.g 3 would be 3 * 2 * 1 which would give you a
total of 6.

I take your code first and start with the mainroutine
-------------------------
Sub Page_load (sender as object, e as EventArgs)
response.write(factorial)
Prints the after that the function factorial is done, the return integer on
a webpage, nothing wrong but only 1 time.
end sub -------------------------
Function Factorial (n as integer) as integer
Because you had not called it with an integer, the n as integer is a fault,
that gives an error
Dim i as integer
for i = 1 to 5
i = i * (i-1)
next i
Return Factorial
When you do this, you would return nothing
End function
-------------------------
I changed it something
\\\\\\
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 5
Response.Write(i.ToString & "=" & Factorial(i) & "<BR>")
Next
'Prints 5 rows from 1 to 5 and start 5 times the function Factorial and send
the value of i to that function.
'The response.write is not so nice in a VB.net webproject but lets not
bother about that, the <br> is a html line feed
End Sub

Function Factorial(ByVal a As Integer) As Integer
'Does the function with the value you suported, now in this funtion it is
called a
Dim i As Integer
For i = 1 To a - 1
If Factorial = 0 Then Factorial = 1
Factorial += Factorial * i
Next
'Figure above out yourself
Return Factorial
'This was your bigest mistake, the Return gives the As integer retour
End Function
////
I don't know if you will read this, so after the rest, but I am curious if
this works ?
I found it funny to do.
Cor
 
You had:
Function Factorial (n as integer) as integer
Dim i as integer
for i = 1 to 5
i = i * (i-1)
next i
Return Factorial

This would recieve an integer and return a result i think:

Function Factorial (n as integer) as integer
Dim intResult as integer = 1
for n= n to 1 step -1 'done it his way to help understanding
intResult = intResult * (n)
next n
Return intResult

dim myinput as integer =Val(textnum.text)
dim myValue as integer
myValue = factorial(myinput)
 
Raphael,
Don't look at my typing and correcting errors please.
Some of them
I shall try to explain you
I shall try to explain this too you
Prints the after that the function factorial is done, the return integer
on
This prints after that the function "factorial" is done, the integer that is
returned etc etc.
:-)
Cor
 
sorry should have been just n, but i just took out some things and left the
() that was already there.

Good catch!!!
 
Back
Top