Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# The % operator

deeps sinha
Greenhorn

Joined: Apr 20, 2012
Posts: 26
hi,
I understand that the % operator gives the remainder but I am not able to understand the result of a (smaller number % bigger number) for eg. 3%6 = 3. please help.

Thanks
Eshwin Sukhdeve
Ranch Hand

Joined: Mar 15, 2012
Posts: 79
% indicates the remander if you say 3%6..then just divide 3 with 6 ..so the remender will be 3 only
Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

Every natural number y (eg 0,1,2,3....) can be described as:
y = a*x + b

In our case we can see that y = 3 and x = 6, so
3 = a*6 + b

When you use the % operator, it will give you the b variable of the above equation. And keeping in mind that we deal only with natural numbers, the result will be:
3 = 0*6 + 3

thus the result equals 3.

I hope this makes things a little more clear for you.
deeps sinha
Greenhorn

Joined: Apr 20, 2012
Posts: 26
Hi Eshwin,
Thanks for helping me. so 3/6 = 6X0+3 so answer is 3.
3/8 = 3x2+2 so answer is 2

am I right?

deeps sinha
Greenhorn

Joined: Apr 20, 2012
Posts: 26
sorry 3/8 = 8x0+3 answer is 3 right?
deeps sinha
Greenhorn

Joined: Apr 20, 2012
Posts: 26
Thanks Panagiotis I now understand this.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1363

Correct

Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

No problem...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 34615

13
Panagiotis Kalogeropoulos wrote:Every natural number y (eg 0,1,2,3....) . . .
Surely that applies to every number, even members of ℝ, so Java™ can use the % operator on floating-point numbers, too.
Rumesh Eranga
Ranch Hand

Joined: Jan 26, 2011
Posts: 33
deeps sinha
Greenhorn

Joined: Apr 20, 2012
Posts: 26
Thanks Everybody !!!

I agree. Here's the link: http://aspose.com/file-tools

subject: The % operator