-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
DecimalToOctal.java
38 lines (32 loc) · 1.06 KB
/
DecimalToOctal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.thealgorithms.conversions;
/**
* This class converts Decimal numbers to Octal Numbers
*/
public final class DecimalToOctal {
private static final int OCTAL_BASE = 8;
private static final int INITIAL_OCTAL_VALUE = 0;
private static final int INITIAL_PLACE_VALUE = 1;
private DecimalToOctal() {
}
/**
* Converts a decimal number to its octal equivalent.
*
* @param decimal The decimal number to convert.
* @return The octal equivalent as an integer.
* @throws IllegalArgumentException if the decimal number is negative.
*/
public static int convertToOctal(int decimal) {
if (decimal < 0) {
throw new IllegalArgumentException("Decimal number cannot be negative.");
}
int octal = INITIAL_OCTAL_VALUE;
int placeValue = INITIAL_PLACE_VALUE;
while (decimal != 0) {
int remainder = decimal % OCTAL_BASE;
octal += remainder * placeValue;
decimal /= OCTAL_BASE;
placeValue *= 10;
}
return octal;
}
}