Contenido del Curso
C# Basics
C# Basics
Practicing For Loop
A factorial of a number is the product of all the numbers from 1
up till that number. For-example the factorial of 5
is the product of all numbers from 1
to 5
(1 x 2 x 3 x 4 x 5
) which gives 120
.
Did You Know?
The mathematical notation for a factorial is
x!
wherex
is any integer. Hence3!
is6
,4!
is24
,5!
is120
and so on. The factorial of0
is1
by definition therefore0!
is1
.
main
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 of2
and continue the loop tilli
is equal tox
, so the loop condition will bei <= x
.
main
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}"); } } }
¡Gracias por tus comentarios!