Skip to content

Commit 7ca8cb7

Browse files
authored
Add Factorial in Dart (#4975)
1 parent e82171c commit 7ca8cb7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

archive/d/dart/factorial.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Issue 4969
2+
void main(List<String> args){
3+
const String error_message = "Usage: please input a non-negative integer";
4+
try{
5+
6+
int factorial = 1;
7+
int num_needed = int.parse(args[0]);
8+
if (num_needed.isNegative == true){
9+
print(error_message);
10+
}
11+
else{
12+
for (int i_index = 1; i_index <= num_needed; i_index ++){
13+
factorial *= i_index;
14+
}
15+
print(factorial.toString());
16+
}
17+
18+
}
19+
catch(e){
20+
print(error_message);
21+
}
22+
}

0 commit comments

Comments
 (0)