How do i write this simple Java program? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do i write this simple Java program?

Write a program that makes change for amounts less than one dollar. Input to the program should be a positive integer less than 100, representing the amount of money in cents. Output should be the original amount of money together with a set of coins (quarters, dimes, nickels, pennies) that could make up that amount. The program should produce change containing the minimum number of coins required for the given amount. The output should be in a natural, non-stilted form. For example, an input of 58 should produce output something like this:

output
58 cents requires 2 quarters, 1 nickel, 3 cents.

Rather than
58 cents requires 2 quarters, 0 dimes, 1 nickels, 3 cents.

Comments

  • AtexAtex Posts: 268Member
    Pseudo code here:
    [code][color=Blue]
    Step0: Enter amount ( N )
    Step1: if N div 25 > 0
    { output ( N div 25 ) + "quarters"
    N = N mod 25 // remainder
    }
    Step2: if N div 10 > 0
    { output ( N div 10 ) + "dimes"
    N = N mod 10
    }
    Step3: if N div 5 > 0
    { output ( N div 5 ) + "nickels"
    N = N mod 5
    }
    Step4: if N > 0
    { output ( N ) + "cents" }
    [/color][/code]

  • Talon876Talon876 Posts: 1Member
    [code]
    import java.util.*;

    public class MakeChange
    {
    int cents;
    int qTaken;
    int dTaken;
    int nTaken;
    int pTaken;
    int init;
    int centsRemaining;

    public MakeChange()
    {

    System.out.println("Input number less than 100 representing cents. ");
    Scanner in = new Scanner(System.in);
    init = Integer.parseInt(in.next());
    cents = init;
    makeChange(cents);
    }

    public void makeChange(int c)
    {
    if (c >= 25)
    {
    while (c >= 25)
    {
    c -= 25;
    qTaken++;
    System.out.println("Took " + qTaken + " quarter(s), now at " + c + " cents left.");
    }
    }
    if (c >= 10)
    {
    while (c >= 10)
    {
    c -= 10;
    dTaken++;
    System.out.println("Took " + dTaken + " dime(s), now at " + c + " cents left.");
    }
    }
    if (c >= 5)
    {
    while (c >= 5)
    {
    c -= 5;
    nTaken++;
    System.out.println("Took " + nTaken + " nickel(s), now at " + c + " cents left.");
    }
    }
    if (c >= 0)
    {
    while (c > 0)
    {
    c -= 1;
    pTaken++;
    System.out.println("Took " + pTaken + " pennies, now at " + c + " cents left.");
    }
    }

    System.out.println(init + " cents takes the following:");
    if(qTaken > 0)
    {
    System.out.println(qTaken + " quarters");
    }
    if(dTaken > 0)
    {
    System.out.println(dTaken + " dimes");
    }
    if(nTaken > 0)
    {
    System.out.println(nTaken + " nickels");
    }
    if(pTaken > 0)
    {
    System.out.println(pTaken + " pennies");
    }


    }

    public static void main(String[] args)
    {
    MakeChange f = new MakeChange();

    }

    }

    [/code]
Sign In or Register to comment.