Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathDecimalToOctal.java
More file actions
Latest commit
38 lines (32 loc) · 1.06 KB
/
Copy pathDecimalToOctal.java
File metadata and controls
38 lines (32 loc) · 1.06 KB
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
packagecom.thealgorithms.conversions;
/**
* This class converts Decimal numbers to Octal Numbers
*/
publicfinalclassDecimalToOctal {
privatestaticfinalintOCTAL_BASE = 8;
privatestaticfinalintINITIAL_OCTAL_VALUE = 0;
privatestaticfinalintINITIAL_PLACE_VALUE = 1;
privateDecimalToOctal() {
}
/**
* 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.
*/
publicstaticintconvertToOctal(intdecimal) {
if (decimal < 0) {
thrownewIllegalArgumentException("Decimal number cannot be negative.");
}
intoctal = INITIAL_OCTAL_VALUE;
intplaceValue = INITIAL_PLACE_VALUE;
while (decimal != 0) {
intremainder = decimal % OCTAL_BASE;
octal += remainder * placeValue;
decimal /= OCTAL_BASE;
placeValue *= 10;
}
returnoctal;
}
}