Here you can find the source of mod(int a, int n)
public static int mod(int a, int n)
//package com.java2s; //License from project: Open Source License public class Main { /** "correct" mod function. */ public static int mod(int a, int n) { if (n < 0) return mod(a, -n); if (n == 0) return a % n; // crash and burn int rtn = a % n; while (rtn < 0) rtn += n;//from w w w . j av a 2 s. c o m return rtn; } }