This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Vamsi Krishna Pasam <[email protected]>
- Loading branch information
1 parent
81e8dfc
commit 7ff3abe
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
program/program/find-sum-of-gp-series/FindSumOfGpSeries.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |