6 喜歡 0 不喜歡
141 瀏覽

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
[正常] Coding (C) - 最新提問 分類:Introduction to Computer Programming I (C) | 用戶: (12.1k 分)
ID: 27174 - 從幾時開始: 無限制 - 到幾時結束: 無限制

標籤修改 用戶: | 141 瀏覽
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.158.154.90
©2016-2024

沒有找到相關問題

12,783 問題
183,443 回答
172,219 留言
4,824 用戶