|
-
Feb 4th, 2004, 04:17 PM
#1
Thread Starter
Member
factorials using for loop [Resolved]
hi
I need a little help I'm doing some work for a class at college, I need to work out the factorial of a number entered in a text box, by using a for loop. I can't use any functions. I'm really not very good at maths I'd really appreciate it if you could offer me any help 
Thanks in advance
- Robert
Edit -
I figured out how to do it. Couldn't believe how easy it is 
VB Code:
Private Sub cmdCalculate_Click()
intNumberInput = Val(txtNumberInput.Text)
intResult = 1
For i = intNumberInput To 1 Step -1
intResult = intResult * i
Next i
lblResult.Caption = intResult
End Sub
Thanks anyway! 
- Robert
Last edited by Robbie_UK; Feb 4th, 2004 at 05:27 PM.
-
Feb 4th, 2004, 05:24 PM
#2
Lively Member
int Factorial(int n)
{
if(n == 0 || n == 1){return 1;}
int r = 1;
for(int i = 2; i <= n; i++)
{
r *= i;
}
return r;
}
Or in English:
if n is 0 or 1, return 1;
multiply the number 1 by each successive integer from 2 to n, and return the result.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|