# Code optimization

Hello all! Im writing a school project and we are supposed to be using only really basic instructions (like if, while, for, etc) and maybe arrays. Could someone tell me how I can optimize some of those instructions? Hit me up if you need a bigger portion of the code. Thank you!

``````var retour = (montant-total).toInt

retour * 100

if(retour/500 != 0) {cinq = retour/500
retour = retour % 500}
if(retour/200 != 0) {deux = retour/200
retour = retour % 200}
if(retour/100 != 0) {un = retour/100
retour = retour % 100}
if(retour/50 != 0) {cinqcts = retour/50
retour = retour % 50}
if(retour/20 != 0) {vingtcts = retour/20
retour = retour % 20}
if(retour/10 != 0) {cinqcts = retour/10
retour = retour % 10}
``````

or

``````if (cinq > 0) {println(cinq + " pièces de 5CHF")}
if (deux > 0) {println(deux + " pièces de 2CHF")}
if (un > 0) {println(un + " pièces de 1CHF")}
if (cinqcts > 0) {println(cinqcts + " pièces de 0.5CHF")}
if (vingtcts > 0) {println(vingtcts + " pièces de 0.2CHF")}
if (dixcts > 0) {println(dixcts + " pièces de 0.1CHF")}
``````

or

``````if ((piece == 5.0) || (piece == 2.0) || (piece == 1.0) || (piece == 0.5) || (piece == 0.2) || (piece == 0.1)) {

if (piece == 5.0) {cinq += 1}
if (piece == 2.0) {deux += 1}
if (piece == 1.0) {un += 1}
if (piece == 0.5) {cinqcts += 1}
if (piece == 0.2) {vingtcts += 1}
if (piece == 0.1) {dixcts += 1}``````

As a general rule you should provide code-examples in English…

Yes im sorry, I can translate it if you want but the name of the variables arent really important here. Its more the pattern

Maybe store coin values and quantities in arrays, e.g.:

``````val pieces = Array(0, 0, 0, 0, 0, 0)
val values = Array(500, 200, 100, 50, 20, 10)

for (i <- 0 to 5) {
if (pieces(i) > 0)
println(pieces(i) + " pieces de " + values(i) / 100.0 + "CHF")
}
``````