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

Commit

Permalink
Resolved issue #3635 (#4807)
Browse files Browse the repository at this point in the history
Co-authored-by: Vamsi Krishna Pasam <[email protected]>
  • Loading branch information
vamsipasam2000 and Vamsi Krishna Pasam authored Dec 1, 2023
1 parent 81e8dfc commit 7ff3abe
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions program/program/find-sum-of-gp-series/FindSumOfGpSeries.fs
Original file line number Diff line number Diff line change
@@ -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<float> =
match Double.TryParse(str) with
| (true, value) -> Some value
| _ -> None

// Function to safely parse int values
let tryParseInt (str: string) : Option<int> =
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)

0 comments on commit 7ff3abe

Please sign in to comment.