Reputation: 316
I want to solve leetcode 172nd question using dart. Take the factorial of a given number and find how many zeros there are at the end.
I done until now
void main() {
print(factorial(5));
}
factorial(int input) {
int factorial = 1;
var answer = 0;
for (int i = 1; i <= input; i++) {
factorial *= i;
}
while (factorial > 10 && factorial.toString().split('').last == "0") {
factorial = (factorial / 10)
answer++;
}
return answer;
}
but when i divide factorial by 10 it not allowed. and if assing at the begining like
double factorial=1;
this time the number is 120.0 and then zero is more. Can anyone help on this, thanks
Upvotes: 0
Views: 344
Reputation: 41793
Check the accepted answer to this Question: How to do Integer division in Dart?
Integer division in Dart has its own operator: ~/
as in
print(16 ~/ 3);
Upvotes: 1
Reputation: 723
You can use method for double to int;
double a = 8.5;
print(a.toInt()) // 8
Answer:
while (factorial > 10 && factorial.toString().split('').last == "0") {
factorial = (factorial / 10).toInt(); // Add toInt()
answer++;
}
Upvotes: 3
Reputation: 2415
To convert a double to int, just use:
double x = 1.256;
print(x.toInt()); //this will print 1
I am not sure what you are asking in this question other than this.
Upvotes: 1