Question:
How to convert pesos to dollars without using multiplication or subtraction?
Code:
public class CurrencyConversion { public static void main(String[] args) { double rate = 0.0741; double pecos = 750.25; double US = pecos / (1/rate); System.out.println("conversion: " + US); // trick for rounding int dollars = (int)US; int cents = (int)(US/.01) % 100; System.out.println( "dollars: " + dollars); System.out.println( "cents: " + cents); } }
Output:
$ java CurrencyConversion conversion: 55.593525 dollars: 55 cents: 59