From b667a14ba379c14064663a7a29165e592137123d Mon Sep 17 00:00:00 2001 From: SaideepKondur Date: Sun, 26 Nov 2023 14:00:37 -0500 Subject: [PATCH 1/4] addition of find-sum-of-squares-of-numbers haskell program --- .../find_sum_of_squares_of_numbers.hs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 program/program/find-sum-of-squares-of-numbers/find_sum_of_squares_of_numbers.hs diff --git a/program/program/find-sum-of-squares-of-numbers/find_sum_of_squares_of_numbers.hs b/program/program/find-sum-of-squares-of-numbers/find_sum_of_squares_of_numbers.hs new file mode 100644 index 000000000..5e0ad3de8 --- /dev/null +++ b/program/program/find-sum-of-squares-of-numbers/find_sum_of_squares_of_numbers.hs @@ -0,0 +1,13 @@ +-- Define a function to calculate the sum of squares +sumOfSquares :: [Int] -> Int +sumOfSquares [] = 0 +sumOfSquares (x:xs) = x^2 + sumOfSquares xs + +-- Main function to read input and print output +main :: IO () +main = do + putStrLn "Enter a list of numbers separated by spaces:" + input <- getLine + let numbers = map read (words input) :: [Int] + let result = sumOfSquares numbers + putStrLn $ "Sum of squares: " ++ show result \ No newline at end of file From 3fbde735d3686c55eadb0cdadb08acb652970d11 Mon Sep 17 00:00:00 2001 From: SaideepKondur Date: Wed, 29 Nov 2023 12:10:11 -0500 Subject: [PATCH 2/4] added rust program to convert_time_from_24-hour_to_12-hour_format --- ...ert_time_from_24-hour_to_12-hour_format.rs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs diff --git a/program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs b/program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs new file mode 100644 index 000000000..401b668a0 --- /dev/null +++ b/program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs @@ -0,0 +1,34 @@ +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!("Invalid input format. Please use HH:MM:SS"); + return; + } + + let hour: u32 = parts[0].parse().expect("Invalid hour"); + let minute: u32 = parts[1].parse().expect("Invalid minute"); + let second: u32 = parts[2].parse().expect("Invalid second"); + + // 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 + ); +} \ No newline at end of file From 02003c31d44669fe0a3da950401a234c5e007c1c Mon Sep 17 00:00:00 2001 From: SaideepKondur <112563014+SaideepKondur@users.noreply.github.com> Date: Fri, 1 Dec 2023 17:48:33 -0500 Subject: [PATCH 3/4] Add files via upload validated hours, minutes and seconds format in Rust program to convert time from 24-hour to 12-hour format --- ...ert_time_from_24-hour_to_12-hour_format.rs | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs b/program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs index 401b668a0..4e3fa5b56 100644 --- a/program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs +++ b/program/program/convert-time-from-24-hour-to-12-hour-format/convert_time_from_24-hour_to_12-hour_format.rs @@ -9,13 +9,34 @@ fn main() { // Parse the input time let parts: Vec<&str> = input_time.trim().split(':').collect(); if parts.len() != 3 { - println!("Invalid input format. Please use HH:MM:SS"); + println!("Error: Invalid input format. Please use HH:MM:SS"); return; } - let hour: u32 = parts[0].parse().expect("Invalid hour"); - let minute: u32 = parts[1].parse().expect("Invalid minute"); - let second: u32 = parts[2].parse().expect("Invalid second"); + // 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; @@ -31,4 +52,4 @@ fn main() { "Converted time: {:02}:{:02}:{:02}{}", formatted_hour, minute, second, period ); -} \ No newline at end of file +} From a555ca5f81e48ef01c80437d507ffe67aab4cf87 Mon Sep 17 00:00:00 2001 From: SaideepKondur <112563014+SaideepKondur@users.noreply.github.com> Date: Sat, 2 Dec 2023 11:19:18 -0500 Subject: [PATCH 4/4] Add files via upload Added Haskell program to print numbers from 1 to n without using a loop #3742. --- ...t_numbers_from_1_to_n_without_using_a_loop.hs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 program/program/print-numbers-from-1-to-n-without-using-a-loop/print_numbers_from_1_to_n_without_using_a_loop.hs diff --git a/program/program/print-numbers-from-1-to-n-without-using-a-loop/print_numbers_from_1_to_n_without_using_a_loop.hs b/program/program/print-numbers-from-1-to-n-without-using-a-loop/print_numbers_from_1_to_n_without_using_a_loop.hs new file mode 100644 index 000000000..eb76274af --- /dev/null +++ b/program/program/print-numbers-from-1-to-n-without-using-a-loop/print_numbers_from_1_to_n_without_using_a_loop.hs @@ -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 \ No newline at end of file