Fakultät einer Zahl berechnen

F = F(tmpWert - 1) * tmpWert
* Unter der Fakultät einer Zahl versteht man die Produkte aller Zahlen verkleinert um 1 vom Wert der Zahl bis 2
  (z.B. 4! = 4 * 3 * 2 = 24).
Private Sub cmdRechnen_Click() Mit anderen Datentypen (Single oder Long) gibt es
Dim Zahl As Double, Ergebnis As Double Fehlermeldungen.
If Len(txtEingabe.Text) = 0 Or IsNumeric(txtEingabe) = False Then Exit Sub
   Zahl = CLng(txtEingabe.Text)
   Ergebnis = F(Zahl) Funktion aufrufen.
   Ausgabe Ergebnis Ausgabe im Bildfeld durchführen.
End Sub
Sub Ausgabe(tmpWert As Variant) Ausgabeposition wird berechnet.
picAusgabe.CurrentX = (picAusgabe.ScaleWidth - picAusgabe.TextWidth(tmpWert)) \ 2
picAusgabe.CurrentY = (picAusgabe.ScaleHeight - picAusgabe.TextHeight(tmpWert)) \ 2
picAusgabe.Print tmpWert Dann die Ausgabe über die Print-Methode.
picAusgabe.Font.Size = 12
picAusgabe.Font.Bold = True
End Sub
Funktion berechnet die Fakultät und ruft sich selbst
Function F(tmpWert As Variant) wieder auf.
If tmpWert < 2 Or tmpWert > 150 Then Ist der Wert im zulässigen Bereich? - Sonst Abbruch.
F = 1 Zuweisung zur Berechnung.
Exit Function
End If
F = F(tmpWert - 1) * tmpWert Funktion mit n-1 rekursiv aufrufen (größte Zahl zuerst).
End Function
Private Sub txtEingabe_Click()
picAusgabe.Cls
txtEingabe.Text = ""
End Sub
   
Eine etwas andere Schreibweise:
Private Sub cmdRechnen_Click()
Dim n As Double, Ergebnis As Double
   n = Val(txtEingabe.Text)
   Ergebnis = fak(n)
   Ausgabe Ergebnis
End Sub
Function fak#(ByVal u_n)
  If u_n < 2 Or u_n > 170 Then
    fak = 1
  Else
    fak = u_n * fak(u_n - 1)
  End If
End Function
Sub Ausgabe(u_n As Double)
  picAusgabe.Print u_n
End Sub
Private Sub txtEingabe_Click()
  picAusgabe.Cls
  txtEingabe.Text = ""
End Sub

 Ranking-Hits zurück Sitemap
Designed by www.wbrnet.info