Skip to content

Instantly share code, notes, and snippets.

@imryan
Created October 11, 2013 17:29
Show Gist options
  • Select an option

  • Save imryan/6938754 to your computer and use it in GitHub Desktop.

Select an option

Save imryan/6938754 to your computer and use it in GitHub Desktop.
Calculates change.
import java.util.Scanner;
class Hello {
public static void main(String[] args) {
// Declare variables
Scanner sc = new Scanner(System.in);
final int QUARTER = 25;
final int DIME = 10;
final int NICKEL = 5;
final int PENNY = 1;
int cents = 0;
int total = 0;
int totalQuarters = 0;
int totalDimes = 0;
int totalNickels = 0;
int totalPennies = 0;
// Get input
cents = sc.nextInt();
// Calculate values
totalQuarters = cents / QUARTER;
cents %= QUARTER;
totalDimes = cents / DIME;
cents %= DIME;
totalNickels = cents / NICKEL;
cents %= NICKEL;
totalPennies = cents / PENNY;
cents %= PENNY;
System.out.println("Quarters:\t" + totalQuarters);
System.out.println("Dimes:\t" + totalDimes);
System.out.println("Nickels:\t" + totalNickels);
System.out.println("Pennies:\t" + totalPennies);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment