Skip to content

Update Quadratic.swift #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions 2-variables/quadratic-formula/Quadratic.swift
Original file line number Diff line number Diff line change
@@ -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)")