Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thmain
Created August 20, 2017 21:41
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 thmain/e2d147b054577113f766d4c5fe1f0d05 to your computer and use it in GitHub Desktop.
Save thmain/e2d147b054577113f766d4c5fe1f0d05 to your computer and use it in GitHub Desktop.
public class FindRemainder {
public static void remainder(int n, int divisor){
if(divisor==0){
System.out.println("Cannot divide by 0");
return;
}
//if either number or divisor is negative
if(n<0)
n *=-1;
if(divisor<0)
divisor *= -1;
int number = n;
//subtract divisor from n till n>=divisor
while(n>=divisor){
n -= divisor;
}
System.out.println("Number: " + number + " , divisor: " + divisor + ". remainder: " + n);
}
public static void main(String[] args) {
int n = 10;
int divisor = 4;
remainder(n, divisor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment