Last active
December 16, 2015 08:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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