Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a program to find remainder of dividing two numbers, without using % operator? In Java

Tags:

java

How to find the remainder of dividing two numbers without using the modulo operator!! My teacher gave me this exact exercise and It's only my 5th lecture in a course called programming fundamentals.
I have already tried this equation,

a%b = a - (a/b)*b

but it always returns zero!

like image 430
Django Avatar asked Jan 26 '14 21:01

Django


1 Answers

I've just tried this

public static void main (String [] args){
    int a = 50;
    int b = 9;

    int c = a%b;
    int d = a - (a/b)*b;

    System.out.println(c);
    System.out.println(d);
}

And it seems to work. What types are your variables?

like image 137
BlueMoon93 Avatar answered Oct 20 '22 10:10

BlueMoon93