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

convert_time_from_24-hour_to_12-hour_format rust program #4789

Merged
merged 8 commits into from
Dec 2, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::io;

fn main() {
// Read input time from the user
let mut input_time = String::new();
println!("Enter time in 24-hour format (HH:MM:SS):");
io::stdin().read_line(&mut input_time).expect("Failed to read line");

// Parse the input time
let parts: Vec<&str> = input_time.trim().split(':').collect();
if parts.len() != 3 {
println!("Error: Invalid input format. Please use HH:MM:SS");
return;
}

// Validate and parse hours, minutes, and seconds
let hour: u32 = match parts[0].parse() {
Ok(value) if value <= 23 => value,
_ => {
println!("Error: Invalid hour");
return;
}
};

let minute: u32 = match parts[1].parse() {
Ok(value) if value <= 59 => value,
_ => {
println!("Error: Invalid minute. Please use minutes between 0 and 59");
return;
}
};

let second: u32 = match parts[2].parse() {
Ok(value) if value <= 59 => value,
_ => {
println!("Error: Invalid second. Please use seconds between 0 and 59");
return;
}
};

// Convert to 12-hour format
let mut formatted_hour = hour % 12;
if formatted_hour == 0 {
formatted_hour = 12;
}

// Determine AM or PM
let period = if hour < 12 { "AM" } else { "PM" };

// Display the result
println!(
"Converted time: {:02}:{:02}:{:02}{}",
formatted_hour, minute, second, period
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
printNumbers :: Int -> IO ()
printNumbers n = go 1
where
go :: Int -> IO ()
go current
| current > n = return ()
| otherwise = do
putStrLn $ show current
go (current + 1)

main :: IO ()
main = do
putStrLn "Enter a number:"
input <- getLine
let n = read input :: Int
printNumbers n