Solution:
#function quadratic
quad= function(a,b,c){
r=(b^2)-(4*a*c)
denom=2*a
#making sure both solution exists first
if (r>0){
#square root part
rad= sqrt(r)
#results
x1=(-b+rad)/denom
x2=(-b-rad)/denom
print(x1)
print(x2)
#if the radical is 0, there will be only 1 solution
}else if(r==0){
denom=2*a
x=-b/denom
print(x)
#If the radical is negative, then we are dealing with imaginary numbers
}else{
real=-b/denom
img=(sqrt(-r))/denom
x1=paste(real,'+', img,'i')
x2=paste(real,'-',img,'i')
print(x1)
print(x2)
}
}
quad(1,2,1)
## [1] -1
quad(1,6,5)
## [1] -1
## [1] -5
quad(1,1,1)
## [1] "-0.5 + 0.866025403784439 i"
## [1] "-0.5 - 0.866025403784439 i"