-4

Yesterday I had to face an interview, in that interview they asked me a question like: Tell me the difference between % and / ?
I know the result what they gave, but I wasn't able to explain it theoretically. Will you please give me any body who can explain it theoretically, I have also done a program on this:

class FindDifference 
{
    public static void main(String[] args) 
    {
        int num1=12;
        int num2=3;
        int modulus=num1%num2;
        int divide=num1/num2;
        System.out.println("modulus :"+modulus);
        System.out.println("division :"+divide);
    }
}

and my result is:

modulus :0    
division :4    

But how can I give the theoretical explanation between them? Will you please help me?

n-anselm
  • 79
  • 5
sreeku24
  • 260
  • 3
  • 4
  • 15
  • 3
    Do you know the definition of division and modulus operation? That should be most of it. – Sourav Ghosh Jan 09 '17 at 16:46
  • 4
    Please don't spam with unrelated tags. If you're programming in Java then only have the Java tag. – Some programmer dude Jan 09 '17 at 16:46
  • Also, please don't spam tags. – Sourav Ghosh Jan 09 '17 at 16:46
  • I know the difference but i don't know how to gave an explanation in interview times – sreeku24 Jan 09 '17 at 16:49
  • I would suggest you to take some time learning before going to any additional interviews. Arithmetical operators are the most fundamental things in *any* programming language... – Eugene Sh. Jan 09 '17 at 16:49
  • 2
    *I know the difference* - you should not know the difference, but the purpose of each one. If you knew what `%` operator does, you wouldn't call it "percentage" but "modulus" or "remainder" in the worst case. – Eugene Sh. Jan 09 '17 at 16:51
  • @MorrisonChang, i don't know how you declared it as a duplicate question, if it duplicate one will you please tag me a correct question. Here am not asking what is % operator , but i want to know the difference between % and / operators. – sreeku24 Jan 09 '17 at 16:54
  • @EugeneSh. Thanks to you to told me one think – sreeku24 Jan 09 '17 at 16:56
  • @sreeku24 the two operators' behaviours are described in the language spec: [Division operator `/`](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.2) and [Remainder operator `%`](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3). But your question "what's the difference" is like asking what's the difference between a knife and a fork: they simply do different things. – Andy Turner Jan 09 '17 at 17:21
  • @AndyTurner there is a difference between the things also what you mentioned there, that i am asking. like by using the knife we can cut the vegetable but fork is used for to eat that pieces – sreeku24 Jan 09 '17 at 17:30

1 Answers1

5

The percent symbol is the modulus operator. I would say that they wanted you to recognize the difference that the division operator on integer inputs gives an integer result. The modulus operator gives the remainder in the sense that the original value is recovered from the integer arithmatic as

(num1/num2)*num2 + num1%num2 == num1

This is the theoretical explanation.

An example (using python for ease of typing) gives

>>> -21 %4
3
>>> -21 /4
-6
>>> -6*4 +3
-21

Your Java program above would give you. (I do not have a Java compiler on this machine).

class FindDifference 
{
    public static void main(String[] args) 
    {
        int num1 = 10;
        int num2 = 3;
        int percentage = num1%num2;
        int divide = num1/num2;
        int resume = divide*num2 + percentage
        System.out.println("Percent :"+percentage);
        System.out.println("division :"+divide);
        System.out.println("resume :"+resume);
    }
}
sabbahillel
  • 3,969
  • 1
  • 17
  • 34