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 7 2212
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
"Raphael Gluck" <iwish i could tell you @alas blame the spammers> wrote in
message news:O7******** ******@TK2MSFTN GP12.phx.gbl... 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
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
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.te xt)
dim myValue as integer
myValue = factorial(myinp ut)
"Herfried K. Wagner [MVP]" <hi*******@m.ac tivevb.de> wrote in message
news:uM******** ******@TK2MSFTN GP12.phx.gbl... Hello,
"Raphael Gluck" <iwish i could tell you @alas blame the spammers> schrieb: 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.
Quick and Dirty:
\\\ Private Function FacIterative(By Val i As Integer) As Integer Dim j As Integer, k As Integer = 1 For j = 1 To i k = k * j Next j Return k End Function ///
-- Herfried K. Wagner MVP · VB Classic, VB.NET http://www.mvps.org/dotnet
"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 errorDim i as integer for i = 1 to 5 i = i * (i-1) next i Return Factorial
When you do this, you would return nothingEnd function
-------------------------
I changed it something
\\\\\\
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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
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
Hello,
"Jeff Brown" <no******@thist ime.com> schrieb: for n= n to 1 step -1 'done it his way to help understanding intResult = intResult * (n)
Why '(n)'?
--
Herfried K. Wagner
MVP · VB Classic, VB.NET http://www.mvps.org/dotnet
sorry should have been just n, but i just took out some things and left the
() that was already there.
Good catch!!!
"Herfried K. Wagner [MVP]" <hi*******@m.ac tivevb.de> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. Hello,
"Jeff Brown" <no******@thist ime.com> schrieb: for n= n to 1 step -1 'done it his way to help understanding intResult = intResult * (n)
Why '(n)'?
-- Herfried K. Wagner MVP · VB Classic, VB.NET http://www.mvps.org/dotnet
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kelly |
last post by:
Hi. I am trying to do something that seems very simple. I want to create a
class that inherits from the ListViewItem Control so that I can add a few
custom properties to it. I then want to make it serializable so that I can
save the ListViewItems contained in the ListView. My code snippet is this:
public class test :...
|
by: Bill |
last post by:
I have a seating chart web form that has over 50 entry field controls
(tables/booths) where I use a DropDownList box to select a single company
name from a single large list of organizations (200 plus conference
attendees). All web form datavalues will be one of the same 200
organizations in this list. I would like to avoid creating 50...
|
by: Phil Certain |
last post by:
Hi,
I'm trying to do something very simple...or at least it should be. I
have created a host page (gen.aspx) and a very simple user control
(us.ascx). The corresponding code-behind files are gen.aspx.vb and
uc.ascx.vb. With simple html or self contained vb in the user control,
everything is fine and dandy. So the next stage is to pass back...
|
by: JimC |
last post by:
On my main form in a C# program, I create an instance of another form that
contains a ListView control, in the usual way, that is:
public class frmMain : System.Windows.Forms.Form
{
//
InfoForm myInfoForm = new InfoForm( );
myInfoForm.Show ( );
|
by: dba_222 |
last post by:
Dear experts,
Again, sorry to bother you again with such a seemingly dumb question,
but I'm having some really mysterious results here.
ie.
Create procedure the_test
As
| |
by: AppleBag |
last post by:
I'm having the worst time trying to login to myspace through code. Can
someone tell me how to do this? Please try it yourself before replying,
only because I have asked this a couple of times in the past in other
places, and while the help was much appreciated, it seemed everyone
just wanted to 'theoretically' explain how to do it, but when I...
|
by: William Foster |
last post by:
Good evening all,
I am creating a program with the ability to select an option from a
ComboBox which will then create a secondary list of options in a
CheckedListBox. Previously when I have needed to create multiples of
this type of user option I have simply hard coded three or four set
options for the user. However, I would like to have...
|
by: John Kotuby |
last post by:
Hi all...
I am trying to do a simple thing and maybe am missing something elementary.
I have created a Javascript function at the top of a page which is meant to
enable editing of an HTML input textbox when the user makes a selection from
the DropDownList control. Since there is no OnClick event I have tried using
OnSelectedIndexChanged or...
|
by: chandan |
last post by:
Hi,
How to create an instance of user control in a class(simple) file not
in Page class file? As I tried I can do it in page class file but not
in simple class file.
Let me know the solution!!
Thanks,
Chandan kumar
|
by: lxyone |
last post by:
Using a flat file containing table names, fields, values whats the
best way of creating html pages?
I want control over the html pages ie
1. layout
2. what data to show
3. what controls to show - text boxes, input boxes, buttons,
hyperlinks ie the usual.
The data is not obtained directly from a database.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |