Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Translation needed

Translation needed

Scheduled Pinned Locked Moved Visual Basic
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oliver_twistor
    wrote on last edited by
    #1

    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 1 Reply Last reply
    0
    • O oliver_twistor

      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 Offline
      I Offline
      Ian Darling
      wrote on last edited by
      #2

      (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.

      O 1 Reply Last reply
      0
      • I Ian Darling

        (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.

        O Offline
        O Offline
        oliver_twistor
        wrote on last edited by
        #3

        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

        I 1 Reply Last reply
        0
        • O oliver_twistor

          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

          I Offline
          I Offline
          Ian Darling
          wrote on last edited by
          #4

          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.

          O 1 Reply Last reply
          0
          • I Ian Darling

            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.

            O Offline
            O Offline
            oliver_twistor
            wrote on last edited by
            #5

            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 had If counter = x Then, and with that code, y was multiplied with 0 in the end, resulting in y = 0. //OT

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups