Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Create ConvertFahrenheitToCelsius.rs #4812

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Rust function to convert fahrenheit to celsius

use std::io;

fn convert_fahrenheit_to_celsius(fahrin: &str) -> String{

let fahrin: f32 = fahrin.trim().parse().expect("Must enter a temperature °C");
let celsius = ((fahrin - 32.0) * 5.0) / 9.0;

let celout = format!("{}", celsius);
return celout;
}

fn main(){
// the input string and this main function can be removed. This is simply for testing

println!("Enter the temperature in fharenheit");
let mut fahrenheit = String::new();
let _rtn = io::stdin().read_line(&mut fahrenheit);

let celsius = convert_fahrenheit_to_celsius(&fahrenheit);

// print out the results of the conversion.
let len = fahrenheit.len();
fahrenheit.truncate(len - 1);
println!();
println!("{}°F is {}°C", fahrenheit, celsius);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Rust function to print an inverted pyramid pattern
//Example:
// Input : 5
// Output :
// 1 2 3 4 5
// 1 2 3 4
// 1 2 3
// 1 2
// 1

use std::io;

fn print_inverted_triangle(n: u32) {

let mut limit = n+1;

for _i in 1 .. n+1 {

for j in 1 .. limit {
print!("{} ", j);
}

limit = limit - 1;
println!();
}
}

fn main() {

// the input string and this main function can be removed. This is simply for testing

println!("Enter the number of levels to print");
let mut input_num = String::new();
let _rtrn = io::stdin().read_line(&mut input_num);
let n: u32 = input_num.trim().parse().expect("Input not an integer");

// example of how to call the function and print out the results of the inverted pyramid.
print_inverted_triangle(n);
}