Pages

Ads 468x60px

Sunday, July 22, 2012

Java: Currency assignment

you will be provided with an amount and you have to find that how can you pay back using
100$ bill, 50$ bill, 20$ bill, 10$ bill, 5$ bill, 1$ bill, quarter (.25), dime (.10), nickel (.05), penny (.01).


import java.util.Scanner;

public class penny
{
 
    public static void main(String[] args) {
   
    System.out.println("Enter money");
    Scanner obj = new Scanner(System.in);
    double  money = obj.nextDouble();
   
    String deci = new String();
    deci = Double.toString(money);
   
    String imp = deci.substring(deci.length()-2);
   
    int decim = Integer.parseInt(imp);
   
    int intg = (int)(money);
   
    int hundred = intg/100;
   
    intg = intg%100;
   
    System.out.println("Hundred Dollar bill \t" + hundred);
   
    int fifty = intg/50;
   
    intg = intg%50;
   
    System.out.println("Fifty Dollar bill \t\t" + fifty);
   
    int twenty = intg/20;
   
    intg = intg%20;
   
    System.out.println("Twenty Dollar bill \t\t" + twenty);
   
    int ten = intg/10;
   
    intg = intg%10;
   
    System.out.println("Ten Dollar bill \t\t" + ten);
   
    int five = intg/5;
   
    intg = intg%5;
   
    System.out.println("Five Dollar bill \t\t" + five);
   
    int one = intg;
   
    System.out.println("One Dollar bill \t\t" + one);
   
    int quarter = decim/25;
   
    decim = decim%25;
   
    System.out.println("Quarters are \t\t\t" + quarter);
   
    int dime = decim/10;
   
    decim = decim%10;
   
    System.out.println("Dimes are \t\t\t" + dime);
   
    int nickel = decim/5;
   
    decim = decim%5;
   
    System.out.println("Nickels are \t\t\t" + nickel);
   
    System.out.println("Pennies are \t\t\t" + decim);
    }
}

No comments:

Post a Comment