From e18ecb60c0911c04d842ab10de1256113c476b7e Mon Sep 17 00:00:00 2001 From: "Luis A.Tavarez" <51054204+uppy19d0@users.noreply.github.com> Date: Sat, 28 Oct 2023 21:43:47 -0400 Subject: [PATCH] Create Factorial.dart We define a function called calculateFactorial that takes an integer n as an argument. In the calculateFactorial function, we use recursion. If n is equal to 0, we return 1 (the factorial of 0 is 1). Otherwise, we multiply n by the result of `calculateFactorial(n - 1). This is what makes the algorithm recursive, as it calls itself with a reduced value in each iteration. In the main function, we define a number (in this case, 5) for which we want to calculate the factorial. You can change this number as needed. Then, we call the calculateFactorial function with the desired number and store the result in the factorial variable. Finally, we print the result using print to display the factorial of the provided number. --- dart/Factorial.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 dart/Factorial.dart diff --git a/dart/Factorial.dart b/dart/Factorial.dart new file mode 100644 index 0000000..6be84f9 --- /dev/null +++ b/dart/Factorial.dart @@ -0,0 +1,13 @@ +int calculateFactorial(int n) { + if (n == 0) { + return 1; + } else { + return n * calculateFactorial(n - 1); + } +} + +void main() { + int number = 5; // You can change this number to any value you want. + int factorial = calculateFactorial(number); + print("The factorial of $number is $factorial"); +}