Created
August 6, 2021 12:39
-
-
Save humamfauzi/7ea4bfad6b1e94e697c7e1bf7022199a to your computer and use it in GitHub Desktop.
Celcius to Farenheit Temperature Conversion In Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let celcius_example: f32 = 100.; | |
let conversion: f32 = converter(celcius_example, "F"); | |
println!("example {}", conversion); | |
let farenheit_example: f32= 212.; | |
let conversion: f32 = converter(farenheit_example, "C"); | |
println!("example {}", conversion); | |
let celcius_example: f32 = 0.; | |
let conversion: f32 = converter(celcius_example, "F"); | |
println!("example {}", conversion); | |
let farenheit_example: f32= 32.; | |
let conversion: f32 = converter(farenheit_example, "C"); | |
println!("example {}", conversion); | |
} | |
fn converter(value: f32, conversion_type: &str) -> f32 { | |
let mut result: f32 = value; | |
if conversion_type == "C" { | |
result = celcius_conversion(value); | |
} else if conversion_type == "F" { | |
result = farenheit_conversion(value); | |
} | |
result | |
} | |
fn celcius_conversion(value :f32) -> f32 { | |
(5./9.) * (value - 32.) | |
} | |
fn farenheit_conversion(value :f32) -> f32 { | |
(9./5.) * value + 32. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment