Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# The % operator

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 90
• Number of slices to send:
Optional 'thank-you' note:
% indicates the remander if you say 3%6..then just divide 3 with 6 ..so the remender will be 3 only

Rancher
Posts: 99
• 1
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
sorry 3/8 = 8x0+3 answer is 3 right?

deeps sinha
Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
Thanks Panagiotis I now understand this.

Bartender
Posts: 1558
5
• Number of slices to send:
Optional 'thank-you' note:
Correct

Panagiotis Kalogeropoulos
Rancher
Posts: 99
• Number of slices to send:
Optional 'thank-you' note:
No problem...

Marshal
Posts: 74783
336
• Number of slices to send:
Optional 'thank-you' note:

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.

Ranch Hand
Posts: 34
• Number of slices to send:
Optional 'thank-you' note: