Skip to content

Instantly share code, notes, and snippets.

@rajdeep26
Last active December 16, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajdeep26/5406557 to your computer and use it in GitHub Desktop.
Save rajdeep26/5406557 to your computer and use it in GitHub Desktop.
Write a function which takes two integers - 'nr' & 'dr' (numerator and denominator) as arguments and *returns* the remainder of the division. You are not allowed to use the built-in remainder operator(s) - %, divmod, etc.
public class Remainder
{
public static int findRemainder(int numerator, int denominator)
{
int result = 0;
if(denominator > numerator)
{
System.out.println("Remainder : "+numerator);
}
else
{
result = denominator;
for(int i=1;denominator*i<= numerator;i++)
result= denominator*i;
}
return (numerator-result);
}
public static void main(String[] args)
{
int remainder = findRemainder(35,4);
System.out.println("Remainder : "+remainder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment