Java Arithmetic Operator remainder operator

Question

We would like to find out the changes for give numbers of money.

The total money we have is 248 cents.

The code should output the quarters, dimes, nickels, cents it include.


public class Main {

  public static void main(String args[]) {
    int total = 248;
    
    //your code here
  }
}



public class Main {

  public static void main(String args[]) {
    int total = 248;
    int quarters = total / 25;
    int whatsLeft = total % 25;

    int dimes = whatsLeft / 10;
    whatsLeft = whatsLeft % 10;

    int nickels = whatsLeft / 5;
    whatsLeft = whatsLeft % 5;

    int cents = whatsLeft;

    System.out.println("From " + total + " cents you get");
    System.out.println(quarters + " quarters");
    System.out.println(dimes + " dimes");
    System.out.println(nickels + " nickels");
    System.out.println(cents + " cents");
  }
}

The symbol for the remainder operator is the percent sign (%).




PreviousNext

Related