What is a remiander operator? Can u please give me any example of program it is used in? Write an application that read two integers, determines whether first is a multiple of second and prints the result. {Use remainder operator}
Remainder Operator?
Page 1 of 13 Replies  12003 Views  Last Post: 09 June 2011  08:18 AM
Replies To: Remainder Operator?
#2
Re: Remainder Operator?
Posted 09 June 2011  03:59 AM
The remainder operator is most often called modulo and it is the percent sign: %
What a lot of people use it for is to check if a number is divisible by another number and they do it like so:
I hope you understand it from that but you should read this: http://www.javaranch...ive/modulo.html
EDIT: Some good practice would be to register on the following site and do the first problem they have up: http://projecteuler....ection=problems
What a lot of people use it for is to check if a number is divisible by another number and they do it like so:
for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { System.out.println(i+"is divisible by two."); } //if the remainder is 0 then it is divisible by two because there is no remainder else { System.out.println(i+"is not divisible by two."); } }
I hope you understand it from that but you should read this: http://www.javaranch...ive/modulo.html
EDIT: Some good practice would be to register on the following site and do the first problem they have up: http://projecteuler....ection=problems
This post has been edited by MATTtheSEAHAWK: 09 June 2011  04:00 AM
#3
Re: Remainder Operator?
Posted 09 June 2011  08:15 AM
That answer is correct. I'd only add that this shows you the distinction between integer and floating point arithmetic: the division operator (/) works differently in the two cases. This may be a bit more explanation than you're looking for, but here's how it works:
When you use an arithmetic operator, the jvm ensures that both arguments are of the same type. To put it simply, if one side is a double, the other side will become a double. The result will be of this same type  although it will also be promoted if the target of assignment requires a higher type.
So if you have two doubles, and you divide them, you get a double, and it's the closest possible representation of the actual value in the double format. (There is a lot concealed here, which I'm skipping over) If you have a double and you divide by an int, or if you divide an int by a double, the int value will become a double (to you and me, this looks like this: 2>2.0 but to the machine, the internal representation is completely different  again, skipping over a lot of complication, which Java hides for you) and then you proceed as above, returning a double.
If you have two ints, however, things are a little different. Now, we have to return an int value, so we do that (for the division m/n) by returning the largest quotient q such that q*n<=m. Thus, 5/4 returns 1. The modulo operator returns the result of m (q*n), which is to say, the remainder of the division.
The difference between floatingpoint and integer division is a common source of error for beginning programmers. Whenever you do division, you should ask yourself which sort you want. One thing to remember is that each operation checks its arguments. If you have a complex piece of arithmetic, this can be confusing, and you'll need to think about the order of operations to make sure you're doing the right thing at the right time.
The Java Language Spec has a fairly comprehensive discussion of these issues: chapter 5 is devoted to conversions and promotions, and the arithmetic operators are specified in grinding detail in chapter 15. It's worth reading at some point, you can get it from Sun as PDF or HTML.
When you use an arithmetic operator, the jvm ensures that both arguments are of the same type. To put it simply, if one side is a double, the other side will become a double. The result will be of this same type  although it will also be promoted if the target of assignment requires a higher type.
So if you have two doubles, and you divide them, you get a double, and it's the closest possible representation of the actual value in the double format. (There is a lot concealed here, which I'm skipping over) If you have a double and you divide by an int, or if you divide an int by a double, the int value will become a double (to you and me, this looks like this: 2>2.0 but to the machine, the internal representation is completely different  again, skipping over a lot of complication, which Java hides for you) and then you proceed as above, returning a double.
If you have two ints, however, things are a little different. Now, we have to return an int value, so we do that (for the division m/n) by returning the largest quotient q such that q*n<=m. Thus, 5/4 returns 1. The modulo operator returns the result of m (q*n), which is to say, the remainder of the division.
The difference between floatingpoint and integer division is a common source of error for beginning programmers. Whenever you do division, you should ask yourself which sort you want. One thing to remember is that each operation checks its arguments. If you have a complex piece of arithmetic, this can be confusing, and you'll need to think about the order of operations to make sure you're doing the right thing at the right time.
The Java Language Spec has a fairly comprehensive discussion of these issues: chapter 5 is devoted to conversions and promotions, and the arithmetic operators are specified in grinding detail in chapter 15. It's worth reading at some point, you can get it from Sun as PDF or HTML.
Page 1 of 1
