From 63c8f808c72c375386abd067664e6bc5b7d934da Mon Sep 17 00:00:00 2001 From: Vamsi Krishna Pasam Date: Thu, 30 Nov 2023 11:11:48 -0500 Subject: [PATCH] Resolved issue #3635 --- .../FindSumOfGpSeries.fs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 program/program/find-sum-of-gp-series/FindSumOfGpSeries.fs diff --git a/program/program/find-sum-of-gp-series/FindSumOfGpSeries.fs b/program/program/find-sum-of-gp-series/FindSumOfGpSeries.fs new file mode 100644 index 000000000..936d0829c --- /dev/null +++ b/program/program/find-sum-of-gp-series/FindSumOfGpSeries.fs @@ -0,0 +1,39 @@ +open System + +let sumOfGP (a: float) (r: float) (n: int) : float = + if r = 1.0 then float n * a + else a * (pown r n - 1.0) / (r - 1.0) + +// Function to safely parse float values +let tryParseFloat (str: string) : Option = + match Double.TryParse(str) with + | (true, value) -> Some value + | _ -> None + +// Function to safely parse int values +let tryParseInt (str: string) : Option = + match Int32.TryParse(str) with + | (true, value) -> Some value + | _ -> None + +// Read inputs from user +printfn "Enter the first term (a):" +let a = + match Console.ReadLine() |> tryParseFloat with + | Some value -> value + | None -> failwith "Invalid input for the first term" + +printfn "Enter the common ratio (r):" +let r = + match Console.ReadLine() |> tryParseFloat with + | Some value -> value + | None -> failwith "Invalid input for the common ratio" + +printfn "Enter the number of terms (n):" +let n = + match Console.ReadLine() |> tryParseInt with + | Some value -> value + | None -> failwith "Invalid input for the number of terms" + +// Calculate and print the sum +printfn "Sum of GP series: %f" (sumOfGP a r n) \ No newline at end of file