title: Can't figure out why I'm getting java.lang.ArithmeticException: / by zero
body: <p>I am trying to create a java program that would take an input amount and output the change with fewest coins. Although when I run my program I get the desired output, I get a unit testing error for the method Change. Here is my code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Main {
      public static void main(String[] args) {
          int totalamount;
          int[] coins = new int[] { 100, 25, 10, 5, 1 };
          Scanner s = new Scanner(System.in);
          totalamount = s.nextInt();
          Change(totalamount, coins);
      }
  
      public static void Change(int totalamount, int[] coins) {
          int[] amount = new int[5]; 
  
          for (int i = 0; i &lt; 5; i++) { 
              if (totalamount &gt;= coins[i]) { 
                  amount[i] = totalamount / coins[i]; 
                  totalamount = totalamount % coins[i]; 
              } 
          }
  
          for (int i = 0; i &lt; 5; i++) { 
              if(amount[i] != 0)
              System.out.println(amount[i]);
          } 
      }
  }
  </code></pre>
  
  <p>and this is the unit testing error I get: </p>
  
  <pre><code>Change(300, coins)
  Your output
  java.lang.ArithmeticException: / by zero
  </code></pre>