42.5k views
4 votes
mips Write a program that asks the user for an integer between 0 and 100 that represents a number of cents. Convert that number of cents to the equivalent number of quarters, dimes, nickels, and pennies. Then output the maximum number of quarters that will fit the amount, then the maximum number of dimes that will fit into what then remains, and so on.

1 Answer

4 votes

Answer:

Step-by-step explanation:

The following program was written in Java. It creates a representation of every coin and then asks the user for a number of cents. Finally, it goes dividing and calculating the remainder of the cents with every coin and saving those values into the local variables of the coins. Once that is all calculated it prints out the number of each coin that make up the cents.

import java.util.Scanner;

class changeCentsProgram {

// Initialize Value of Each Coin

final static int QUARTERS = 25;

final static int DIMES = 10;

final static int NICKELS = 5;

public static void main (String[] args) {

int cents, numQuarters,numDimes, numNickels, centsLeft;

// prompt the user for cents

Scanner in = new Scanner(System.in);

System.out.println("Enter total number of cents (positive integer): ");

cents = in.nextInt();

System.out.println();

// calculate total amount of quarter, dimes, nickels, and pennies in the change

centsLeft = cents;

numQuarters = cents/QUARTERS;

centsLeft = centsLeft % QUARTERS;

numDimes = centsLeft/DIMES;

centsLeft = centsLeft % DIMES;

numNickels = centsLeft/NICKELS;

centsLeft = centsLeft % NICKELS;

// display final amount of each coin.

System.out.print("For your total cents of " + cents);

System.out.println(" you have:");

System.out.println("Quarters: " + numQuarters);

System.out.println("Dimes: " + numDimes);

System.out.println("Nickels: " + numNickels);

System.out.println("Pennies: " + centsLeft);

System.out.println();

}

}

mips Write a program that asks the user for an integer between 0 and 100 that represents-example-1
User The Machine
by
3.4k points