### Java Math ceilMod() Method

The `Math.ceilMod()` method in Java is used to return the ceiling modulus of the given arguments.

1. Introduction
2. `ceilMod()` Method Syntax
3. Overloaded `ceilMod()` Methods
4. Examples
• `ceilMod(int x, int y)`
• `ceilMod(long x, int y)`
• `ceilMod(long x, long y)`
5. Real-World Use Case
6. Conclusion

## Introduction

The `Math.ceilMod()` method returns the ceiling modulus of the given arguments. The ceiling modulus operation is the remainder of the division of one number by another, ensuring that the result is always positive.

## ceilMod() Method Syntax

The syntax for the `ceilMod()` method varies depending on the types of the arguments:

### `ceilMod(int x, int y)`

``````public static int ceilMod(int x, int y)
``````

### `ceilMod(long x, int y)`

``````public static int ceilMod(long x, int y)
``````

### `ceilMod(long x, long y)`

``````public static long ceilMod(long x, long y)
``````

### Parameters:

• `x`: The dividend.
• `y`: The divisor.

### Returns:

• The ceiling modulus of the given arguments.

### Throws:

• `ArithmeticException` if the divisor `y` is zero.

## Overloaded ceilMod() Methods

The `Math.ceilMod()` method is overloaded to handle different combinations of primitive data types: `int` and `long`. Each version returns the ceiling modulus of the given arguments.

## Examples

### `ceilMod(int x, int y)`

The `ceilMod(int x, int y)` method returns the ceiling modulus of two `int` values.

#### Example

``````public class CeilModIntExample {
public static void main(String[] args) {
int x1 = 7, y1 = 3;
int x2 = -7, y2 = 3;

int result1 = Math.ceilMod(x1, y1);
int result2 = Math.ceilMod(x2, y2);

System.out.println("Ceiling modulus of " + x1 + " % " + y1 + " is " + result1);
System.out.println("Ceiling modulus of " + x2 + " % " + y2 + " is " + result2);
}
}
``````

Output:

``````Ceiling modulus of 7 % 3 is 1
Ceiling modulus of -7 % 3 is 2
``````

### `ceilMod(long x, int y)`

The `ceilMod(long x, int y)` method returns the ceiling modulus of a `long` and an `int` value.

#### Example

``````public class CeilModLongIntExample {
public static void main(String[] args) {
long x1 = 10L, y1 = 3;
long x2 = -10L, y2 = 3;

int result1 = Math.ceilMod(x1, y1);
int result2 = Math.ceilMod(x2, y2);

System.out.println("Ceiling modulus of " + x1 + " % " + y1 + " is " + result1);
System.out.println("Ceiling modulus of " + x2 + " % " + y2 + " is " + result2);
}
}
``````

Output:

``````Ceiling modulus of 10 % 3 is 1
Ceiling modulus of -10 % 3 is 2
``````

### `ceilMod(long x, long y)`

The `ceilMod(long x, long y)` method returns the ceiling modulus of two `long` values.

#### Example

``````public class CeilModLongLongExample {
public static void main(String[] args) {
long x1 = 20L, y1 = 4L;
long x2 = -20L, y2 = 4L;

long result1 = Math.ceilMod(x1, y1);
long result2 = Math.ceilMod(x2, y2);

System.out.println("Ceiling modulus of " + x1 + " % " + y1 + " is " + result1);
System.out.println("Ceiling modulus of " + x2 + " % " + y2 + " is " + result2);
}
}
``````

Output:

``````Ceiling modulus of 20 % 4 is 0
Ceiling modulus of -20 % 4 is 0
``````

## Real-World Use Case

### Handling Negative Values

In real-world scenarios, the `Math.ceilMod()` method can be used to ensure that the result of a modulus operation is always positive, which is useful in applications like array indexing or circular buffers where negative indices are not valid.

#### Example

``````public class CircularBufferExample {
public static void main(String[] args) {
int bufferSize = 5;
int[] buffer = new int[bufferSize];
int index = -3;

int validIndex = Math.ceilMod(index, bufferSize);

System.out.println("Valid index for buffer is " + validIndex);
}
}
``````

Output:

``````Valid index for buffer is 2
``````

## Conclusion

The `Math.ceilMod()` method in Java provides a way to perform modulus operations and ensure that the result is always positive. By understanding how to use this method and its overloaded versions, you can handle various modulus operations and solve problems that require a positive result. Whether you are working with integers or long integers, the `ceilMod()` method offers a reliable tool for ensuring correct and positive modulus results.