mardi 25 octobre 2016

C++ Large numbers power of 10

New to C++ programming. Is there a way make the code better so that it does not have repeated code.

if (totalDistance < pow(10, 3)) {
    cout << "\nTotal (approx) travel distance = " << totalDistance << " million km\n" << endl;
}
else if (totalDistance < pow(10, 6)) {
    totalDistance = totalDistance / pow(10, 3);
    cout << "\nTotal (approx) travel distance = " << totalDistance << " billion km\n" << endl;
}
else if (totalDistance < pow(10, 9)) {
    totalDistance = totalDistance / pow(10, 6);
    cout << "\nTotal (approx) travel distance = " << totalDistance << " trillion km\n" << endl;
}

Aucun commentaire:

Enregistrer un commentaire