Défi : Méthode Avec Paramètres
In this challenge you need to create a new method called factorial which will have one parameter called n of type int. It should calculate the factorial of the passed value n and output the result.
The blueprint of the program is given, fill the missing details to complete the program:
main.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { internal class Program { static void factorial(___) { if(n == 0) { Console.WriteLine(___); } else if (n > 0) { int result = 1; for (int i = 2; i <= ___; i++) { ___ } Console.WriteLine(___); } else { Console.WriteLine(___); } } static void Main(string[] args) { factorial(-1); factorial(0); factorial(5); } } }
- We had done a factorial related exercise in one of the previous sections but to recap, the factorial of a number is the product of all the numbers up till that number.
For-example the factorial of
5is1 x 2 x 3 x 4 x 5which is120, similarly the factorial of4is24. There are two additional rules:- Factorial of
0is1; - Factorial of a negative number is
undefinedso it should outputundefined.
- Factorial of
- For solving the task, following the following steps:
- Fill in the blank for the
factorialmethod with appropriate parameter definition. It expects an integer. Figure out what should be the name of the parameter by reading the method's inner code; - If the passed argument is
0, it should show a fixed output which is mentioned in the hints; - The loop should continue up till (including the number) the passed integer. The
resultvariable should be updated with the product of itself with the loop variable. In short:result *= i; - If the input number is negative (invalid), it should output "undefined".
- Fill in the blank for the
main.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { internal class Program { static void factorial(int n) { if(n == 0) { Console.WriteLine(1); } else if (n > 0) { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } Console.WriteLine(result); } else { Console.WriteLine("undefined"); } } static void Main(string[] args) { factorial(-1); factorial(0); factorial(5); } } }
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 1.59
Défi : Méthode Avec Paramètres
Glissez pour afficher le menu
In this challenge you need to create a new method called factorial which will have one parameter called n of type int. It should calculate the factorial of the passed value n and output the result.
The blueprint of the program is given, fill the missing details to complete the program:
main.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { internal class Program { static void factorial(___) { if(n == 0) { Console.WriteLine(___); } else if (n > 0) { int result = 1; for (int i = 2; i <= ___; i++) { ___ } Console.WriteLine(___); } else { Console.WriteLine(___); } } static void Main(string[] args) { factorial(-1); factorial(0); factorial(5); } } }
- We had done a factorial related exercise in one of the previous sections but to recap, the factorial of a number is the product of all the numbers up till that number.
For-example the factorial of
5is1 x 2 x 3 x 4 x 5which is120, similarly the factorial of4is24. There are two additional rules:- Factorial of
0is1; - Factorial of a negative number is
undefinedso it should outputundefined.
- Factorial of
- For solving the task, following the following steps:
- Fill in the blank for the
factorialmethod with appropriate parameter definition. It expects an integer. Figure out what should be the name of the parameter by reading the method's inner code; - If the passed argument is
0, it should show a fixed output which is mentioned in the hints; - The loop should continue up till (including the number) the passed integer. The
resultvariable should be updated with the product of itself with the loop variable. In short:result *= i; - If the input number is negative (invalid), it should output "undefined".
- Fill in the blank for the
main.cs
1234567891011121314151617181920212223242526272829303132333435using System; namespace ConsoleApp { internal class Program { static void factorial(int n) { if(n == 0) { Console.WriteLine(1); } else if (n > 0) { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } Console.WriteLine(result); } else { Console.WriteLine("undefined"); } } static void Main(string[] args) { factorial(-1); factorial(0); factorial(5); } } }
Merci pour vos commentaires !