FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Money.java




Java. My first ever Java program.

* INPUT : Double value, i.e., 8465.08 (rendered in Peso)
OUTPUT : Number of bills/coins (1000, 500, 200, 100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01) needed to fill the value.
e.g.
Input: 8456.08
Output:
8 - P1000.0
0 - P500.0
2 - P200.0
0 - P100.0
1 - P50.0
0 - P20.0
0 - P10.0
1 - P5.0
1 - P1.0
0 - P0.25
0 - P0.1
1 - P0.05
3 - P0.01

********
* CODE *
********
Code:
import java.io.*;
import java.text.DecimalFormat;
 
public class Money {
     public static void main(String args[]) {
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          DecimalFormat df = new DecimalFormat("#.##");
 
          double inp = 0.00;
          double[] money = {1000, 500, 200, 100, 50, 20,
          10, 5, 1, 0.25, 0.10, 0.05, 0.01};
 
          int[] results = new int[13];
          int count = 0;
 
          //Get input from user
          System.out.print("Input: ");
          try {
              inp = Double.parseDouble(in.readLine());
          }
          catch(IOException ioe) {
               System.out.println("An error occured. Please try again.");
               System.exit(1);
          }
 
          //Process and print
          int i = 0;
          for(i = 0; i < money.length; i++) {
               while(inp >= money[i]) {
                    inp -= money[i];
                    inp = Double.valueOf(df.format(inp));
                    count++;
               }
               results[i] = count;
               count = 0;
          }
 
          System.out.println("Output:");
          for(i = 0; i < results.length; i++) {
               System.out.println(results[i] + " - P" + money[i]);
          }
     }
}




0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.