Translation needed
-
I have tried to figure something out that I want to write in code, but I couldn't find the solution. Until now. I found the following piece of code on the Internet today. Unfortunately, it is in the language of C (I think so), a language that I don't know anything about. So, I would be very grateful if anyone could translate it to Visual Basic code.
#include iostream.h //Fakultetsfunktion int fak(int n); void main() { int x=5; cout<< x << "!=" << endl; cout<< fak(x) << endl; cout<< 5*4*3*2*1 << endl; } //Rekursiv fakultetsfunktion int fak(int n) { if (n<=0) return 1; else { int a = n*fak( n-1 ); return (a); } }
//OT -
I have tried to figure something out that I want to write in code, but I couldn't find the solution. Until now. I found the following piece of code on the Internet today. Unfortunately, it is in the language of C (I think so), a language that I don't know anything about. So, I would be very grateful if anyone could translate it to Visual Basic code.
#include iostream.h //Fakultetsfunktion int fak(int n); void main() { int x=5; cout<< x << "!=" << endl; cout<< fak(x) << endl; cout<< 5*4*3*2*1 << endl; } //Rekursiv fakultetsfunktion int fak(int n) { if (n<=0) return 1; else { int a = n*fak( n-1 ); return (a); } }
//OT(Untested and VB.NET, so replace Integer with Long in VB6)
Function Factorial(ByVal n As Integer) As Integer ' AFAIK, you can actually do this as <= 1 instead If n <= 0 then return 1 else return n * Factorial(n - 1) end if End Function
You might want to read up on recursive functions, as that is all this is. -- Ian Darling If I was any more loopy, I'd be infinite. -
(Untested and VB.NET, so replace Integer with Long in VB6)
Function Factorial(ByVal n As Integer) As Integer ' AFAIK, you can actually do this as <= 1 instead If n <= 0 then return 1 else return n * Factorial(n - 1) end if End Function
You might want to read up on recursive functions, as that is all this is. -- Ian Darling If I was any more loopy, I'd be infinite.So there is a function to do n-factorial. I didn't know that. Well, I was hoping to find out how to write a for...while loop or something that does the same thing. I have spoken with my mathematics teacher and we are trying to find some algorithm to do n-factorial. I mean, a calculator doesn't know what, for example, "5!" is, does it? I tried to write a loop with "counter" that added "1" for every loop, and when "counter" had the same value as "n". It did not work, though. But thanks for the help, anyway. //OT
-
So there is a function to do n-factorial. I didn't know that. Well, I was hoping to find out how to write a for...while loop or something that does the same thing. I have spoken with my mathematics teacher and we are trying to find some algorithm to do n-factorial. I mean, a calculator doesn't know what, for example, "5!" is, does it? I tried to write a loop with "counter" that added "1" for every loop, and when "counter" had the same value as "n". It did not work, though. But thanks for the help, anyway. //OT
You can work out a factorial in a loop too (again VB.NET):
Dim total as Integer = 1 ' starting at 1 is important here For i as Integer = 2 to 5 ' replace the 5 with a variable as necessary total = total * i Next i
-- Ian Darling If I was any more loopy, I'd be infinite. -
You can work out a factorial in a loop too (again VB.NET):
Dim total as Integer = 1 ' starting at 1 is important here For i as Integer = 2 to 5 ' replace the 5 with a variable as necessary total = total * i Next i
-- Ian Darling If I was any more loopy, I'd be infinite.Thanks, but I have already worked it out. It was only one small adjustment I had to do in my code to make it worked. I did it like this:
Dim x As Integer Dim y As Integer Dim counter As Integer Dim check As Boolean Private Sub Bttn_Fkltt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bttn_Fkltt.Click x = TxtBx_Fkltt.Text counter = 1 check = False y = x * (x - 1) If x > 2 Then Do Until check = True counter = counter + 1 y = y * (x - counter) If counter = x - 1 Then check = True Exit Do End If Loop Lbl_Fkltt.Text = y Else Lbl_Fkltt.Text = y End If End Sub
Look at the code:If counter = x - 1 Then
. Before, I hadIf counter = x Then
, and with that code,y
was multiplied with 0 in the end, resulting in y = 0. //OT