Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Herausforderung: For-Schleife | Schleifen
C# Grundlagen
course content

Kursinhalt

C# Grundlagen

C# Grundlagen

1. Erste Schritte
2. Umgang mit Datentypen
3. Kontrollstrukturen
4. Schleifen
5. Arrays
6. Methoden

book
Herausforderung: For-Schleife

Eine Fakultät einer Zahl ist das Produkt aller Zahlen von 1 bis zu dieser Zahl. Zum Beispiel ist die Fakultät von 5 das Produkt aller Zahlen von 1 bis 5 (1 x 2 x 3 x 4 x 5), was 120 ergibt.

cs

main

copy
12345678910111213141516171819202122232425262728
using System; namespace ConsoleApp { class Program { static void Main(string[] args) { int x = 5; int result = ___; if(x == 0) { result = ___; } else { for (___) { result *= ___; } } Console.WriteLine($"Factorial of {x} is {result}"); } } }

Initialisieren Sie die Schleifenvariable i mit einem Wert von 2 und setzen Sie die Schleife fort, bis i gleich x ist, sodass die Schleifenbedingung i <= x lautet.

cs

main

copy
12345678910111213141516171819202122232425262728
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int x = 5; int result = 1; if(x == 0) { result = 0; } else { for (int i = 2; i <= x; i++) { result *= i; } } Console.WriteLine($"Factorial of {x} is {result}"); } } }

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
We're sorry to hear that something went wrong. What happened?
some-alt