Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: for Loop | Loops
C# Basics

bookChallenge: for Loop

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

main.cs

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}"); } } }

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

main.cs

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}"); } } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 1.59

bookChallenge: for Loop

Pyyhkäise näyttääksesi valikon

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

main.cs

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}"); } } }

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

main.cs

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}"); } } }
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 2
some-alt