Quadratic equation in Dart
Example for versions
Dart 1.1.1
import 'dart:io';
import 'dart:math' show sqrt;
int readInt() {
String input = stdin.readLineSync();
return int.parse(input);
}
main() {
int A, B, C;
try {
A = readInt();
B = readInt();
C = readInt();
}
on FormatException {
print("Coefficient is not a number.");
return;
}
if (A == 0) {
print("Not a quadratic equation.");
return;
}
int D = B * B - 4 * A * C;
double p1 = - B / 2.0 / A;
double p2 = sqrt(D.abs()) / 2.0 / A;
if (D == 0) {
print("x = $p1");
} else {
if (D > 0) {
print("x1 = ${p1 + p2}");
print("x2 = ${p1 - p2}");
} else {
print("x1 = ($p1, $p2)");
print("x2 = ($p1, ${-p2})");
}
}
}
Comments
]]>blog comments powered by Disqus
]]>