Anda di halaman 1dari 2

Like Us On Facebook & Follow Us On Twitter

Like Share 153 people like this. Sign Up to see what your friends like. Follow @CodingBotBlog

Select Language

Pow ered by

Translate

We Provide Programming Codes Used In Various Techniques Of C And C++.


Home About Contact Subscribe Privacy Policy

Tuesday, 19 November 2013

Calculate Roots Of a Quadratic Equation Using C Program


SOCIALIZE IT
Tw eet 1 Like 1 2

Hi everyone In this post, we're going how to write a C program to calculate the roots of a quadratic equation. As we know that a quadratic equation is of a form like:

ax^2+bx+c=0
So we won't go into much details. Also you guys must be knowing to calculate the roots of a quadratic equation directly, having formula:

Roots = ( -b + (b^2 - 4ac) ) / 2a & ( -b - (b^2 - 4ac) ) / 2a


So we are just going to use the same formula in our C code. So Let's begin...

C Program To Calculate Roots Of a Quadratic Equation:


1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 / *D o u b l e C l i c kT oS e l e c tC o d e* / # i n c l u d e < s t d i o . h > # i n c l u d e < c o n i o . h > # i n c l u d e < m a t h . h > v o i dm a i n ( ) { f l o a ta , b , c , r o o t 1 , r o o t 2 , x , r e a l , i m ; c l r s c r ( ) ; p r i n t f ( " >Q u a d r a t i cE q u a t i o nF o r m :a x 2 + b x + c " ) ; p r i n t f ( " \ n \ n E n t e rt h ev a l u eo fa :" ) ; s c a n f ( " % f " , & a ) ; p r i n t f ( " \ n E n t e rt h ev a l u eo fb :" ) ; s c a n f ( " % f " , & b ) ; p r i n t f ( " \ n E n t e rt h ev a l u eo fc :" ) ; s c a n f ( " % f " , & c ) ; x=( b * b ) ( 4 * a * c ) ; i f ( x > 0 ) { r o o t 1=( b + s q r t ( x ) ) / ( 2 * a ) ; r o o t 2=( b s q r t ( x ) ) / ( 2 * a ) ; / / R O O T SA R EU N I Q U E p r i n t f ( " \ n T h er o o t so ft h ee q u a t i o na r e :% . 2 fa n d% . 2 f " , r o o t 1 , r o o t 2 ) ; } e l s ei f ( x = = 0 ) { r o o t 1=b / ( 2 * a ) ; r o o t 2=r o o t 1 ; / / R O O T SA R ES A M E p r i n t f ( " \ n T h er o o t so ft h ee q u a t i o na r e :% . 2 fa n d% . 2 f " , r o o t 1 , r o o t 2 ) ; } e l s e { r e a l=b / ( 2 * a ) ; i m=s q r t ( x ) / ( 2 * a ) ; / /R O O T SA R EC O M P L E X p r i n t f ( " \ n T h er o o t so ft h ee q u a t i o na r e :% . 2 f + j % . 2 fa n d% . 2 f j % . 2 f " , r e a l , i m , r e a l , i m ) ; }
?

4 2 4 3 4 4

g e t c h ( ) ; }

Program Explanation:
The program is self explanatory in itself. First we take the coefficients of the quadratic equation as
Enjoy this page? Like us on Facebook! input from the user. Then on the basis of its determinant,i.e, b^2 - 4*a*c, if its greater than 0 or Like 153 equal to 0 or less than 0. In each of the case, we can get different types of roots, Unique, Equal or Complex respectively. Using the formula as I described in the beginning of this post, we calculate roots, with an exception in case of complex roots, whereby, we have to find real and imaginary parts of the roots individually, using the formula: Real Part = -b/(2*a) Imaginary Part = sqrt(-x)/(2*a) -> Since determinant is negative here, we make it positive by multiplying an extra '-'.

Program Output:

P l e a s eC o m m e n tI fY o uL i k e dT h eP o s t .
Do you like this post? Please link back to this article by copying one of the codes below. URL:
h t t p : / / w w w . c o d i n g b o t . n e t / 2 0 1 3 / 1 1 / c a l c u l a t e r o o t s o f q u a d r a t i c e q u a t i o n . h t m l

HTML link code:


< ah r e f = " h t t p : / / w w w . c o d i n g b o t . n e t / 2 0 1 3 / 1 1 / c a l c u l a t e r o o t s o f q u a d r a t i c e q u a t i o n . h t m l " > C a l c u l a t e R o o t sO faQ u a d r a t i cE q u a t i o nU s i n gCP r o g r a m < / a >

Forum link code:


[ u r l = h t t p : / / w w w . c o d i n g b o t . n e t / 2 0 1 3 / 1 1 / c a l c u l a t e r o o t s o f q u a d r a t i c e q u a t i o n . h t m l ] C a l c u l a t eR o o t s O faQ u a d r a t i cE q u a t i o nU s i n gCP r o g r a m [ / u r l ]

Anda mungkin juga menyukai