Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Challenge 2 – CountMoney.java Write a program that classifies a given amount of

ID: 3787984 • Letter: C

Question

Challenge 2 – CountMoney.java

Write a program that classifies a given amount of money into smaller monetary units. The program lets the user enter an amount as an integer value, in which the last two digits represent the cents (Remember that you do not want to use float or double for currency operations due to rounding issues). For example, the input 1156 represents 11 dollars and 56 cents. The program outputs a list on the monetary equivalent in dollars, quarters, dimes, nickels, and pennies.

Sample output: Enter an amount in integer, for example 1156 for 11 dollars and 56 cents: 359865 Your amount 359865 consists of 3598 dollars 2 quarters 1 dimes 1 nickels 0 pennies

Explanation / Answer

import java.util.Scanner;
public class CountMoney{

public static void main(String []args){
int amount;
int pennies=1,dime=10,nickel=5,quarter=25;
Scanner sc=new Scanner(System.in);
    System.out.println("Enter your amount");
    amount=sc.nextInt();
//Extracting The Last 2 Digit
int endDigit=(amount%100);
//Extracting Total Dollar From Amount
int totalDollar=(amount/100);
//Get Total Quarter From Amount
int totalQuarter=(endDigit)/quarter;
endDigit=(endDigit%quarter);
//Get Total Dime From Amount
int totalDime=(endDigit)/dime;
endDigit=(endDigit%dime);
int totalNickel=(endDigit)/nickel;
endDigit=(endDigit%nickel);
int totalPennies=(endDigit)/pennies;
endDigit=(endDigit%pennies);
System.out.println("Dollor: "+totalDollar+" Quarter: "+totalQuarter+ " Dime: "+totalDime+" Nickel: "+totalNickel+" Pennies: "+totalPennies);
}
}