6 like 0 dislike
135 views

Write a program to find roots of a quadratic equation

quadratic equation is a second order equation having a single variable. Any quadratic equation can be represented as , where a, b and c are constants ( a can't be 0) and x is unknown variable. 

For Example

 is a quadratic equation where a, b and c are 2, 5 and 3 respectively.

To calculate the roots of quadratic equation we can use below formula. There are two solutions of a quadratic equation.

x = (-b + sqrt(D))/(2*a)
x = (-b - sqrt(D))/(2*a)

where, D = (b*b-4*a*c) is Discriminant, which differentiate the nature of the roots of quadratic equation.

For the complex result:

realPart = -b/(2*a);
imaginaryPart =sqrt(-D)/(2*a);

Note: We have used sqrt() function to find square root which is in math.h library.

 

Example input 1:

1 2 1

Example output 1:

Roots of 1.00x^2 + 2.00x + 1.00 = 0 are real and same
x1 = x2 = -1.00

 

Example input 2:

1 -3 2

Example output 2:

Roots of 1.00x^2 + -3.00x + 2.00 = 0 are real and different
x1 = 2.00
x2 = 1.00

 

Example input 3:

1 2 2

Example output 3:

Roots of 1.00x^2 + 2.00x + 2.00 = 0 are complex and different
x1 = -1.00+1.00i
x2 = -1.00-1.00i
[Normal] Coding (C) - asked in Introduction to Computer Programming I (C) by (12.1k points)
ID: 27174 - Available when: Unlimited - Due to: Unlimited

retagged by | 135 views
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.42.144
©2016-2024

No related questions found

12,783 questions
183,443 answers
172,219 comments
4,824 users