From 647ef39b380fcf5df7f0ac6d43a58332e613a749 Mon Sep 17 00:00:00 2001 From: Mo-ha-mm-ed <100417507+Mo-ha-mm-ed@users.noreply.github.com> Date: Wed, 21 Jun 2023 22:11:30 +0300 Subject: [PATCH] Update Quadratic.swift Another way of doing the project --- 2-variables/quadratic-formula/Quadratic.swift | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/2-variables/quadratic-formula/Quadratic.swift b/2-variables/quadratic-formula/Quadratic.swift index 0fb4379..9b8e814 100644 --- a/2-variables/quadratic-formula/Quadratic.swift +++ b/2-variables/quadratic-formula/Quadratic.swift @@ -1,19 +1,39 @@ -// Quadratic Formula 📈 -// Sonny Li +/* +Date: June 21, 2023 -var a: Double = 6 -var b: Double = -7 -var c: Double = -3 +This is to calculate quadratic formula. -var root1: Double -var root2: Double +The equation is: -// The positive root -root1 = (-b + (b*b - 4*a*c).squareRoot()) / (2*a) +x = (-b +or- sqrt(b^2 - 4ac) )/(2a) + +First step let us defind b, a, c +*/ + +import Foundation //Math framwork + +//Identify a, b, c +var a : Double = 2 + +var b : Double = 5 + +var c : Double = 3 + + +//b^2 +var bSquared = pow(b, 2) + + +// square root +var discriminant : Double = sqrt(bSquared - (4 * a * c)) + + +//The formula +var xPositive : Double = ((-b + discriminant) / (2 * a)) +var xNegitive : Double = ((-b - discriminant) / (2 * a)) + +//Print Result +print("The quadratic formula result for X if a = \(a) b = \(b) and c = \(c) is:") +print(" X = \(xPositive), or X = \(xNegitive)") -// The negative root -root2 = (-b - (b*b - 4*a*c).squareRoot()) / (2*a) -// Outputting the roots -print("Root 1 is \(root1)") -print("Root 2 is \(root2)")