-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathdecimal_to_octal.rs
More file actions
37 lines (31 loc) · 945 Bytes
/
decimal_to_octal.rs
File metadata and controls
37 lines (31 loc) · 945 Bytes
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
// Author: NithinU2802
// Decimal to Octal Converter: Converts Decimal to Octal
// Wikipedia References:
// 1. https://en.wikipedia.org/wiki/Decimal
// 2. https://en.wikipedia.org/wiki/Octal
pub fn decimal_to_octal(decimal_num: u64) -> String {
if decimal_num == 0 {
return "0".to_string();
}
let mut num = decimal_num;
let mut octal = String::new();
while num > 0 {
let remainder = num % 8;
octal.push_str(&remainder.to_string());
num /= 8;
}
// Reverse the string to get the correct octal representation
octal.chars().rev().collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_decimal_to_octal() {
assert_eq!(decimal_to_octal(8), "10");
assert_eq!(decimal_to_octal(15), "17");
assert_eq!(decimal_to_octal(255), "377");
assert_eq!(decimal_to_octal(100), "144");
assert_eq!(decimal_to_octal(0), "0");
}
}