Uitdaging: For-Lus
Make a program that calculates the factorial of a given number x using a for loop. If the number is 0, the program should return 1.
main.cs
12345678910111213141516171819202122232425262728using 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}"); } } }
Initialize the loop variable i with a value of 2 and continue the loop till i is equal to x, so the loop condition will be i <= x.
main.cs
12345678910111213141516171819202122232425262728using 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}"); } } }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you show me an example of how the program should work with a specific number?
What should the program do if the input is a negative number?
Can you explain why the loop starts at 2 instead of 1?
Awesome!
Completion rate improved to 1.59
Uitdaging: For-Lus
Veeg om het menu te tonen
Make a program that calculates the factorial of a given number x using a for loop. If the number is 0, the program should return 1.
main.cs
12345678910111213141516171819202122232425262728using 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}"); } } }
Initialize the loop variable i with a value of 2 and continue the loop till i is equal to x, so the loop condition will be i <= x.
main.cs
12345678910111213141516171819202122232425262728using 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}"); } } }
Bedankt voor je feedback!