Pages

When Programming is Life

Wednesday, 11 February 2015

Find Roots of Quadratic Equation in C Programming

Find Roots of Quadratic Equation in C Programming
Find Roots of Quadratic Equation in C Programming


Hello Programmers, welcome back to Standard Programming. In this post I am going to show you guys how to find the roots of a quadratic equation in C Programming. Remember the process of finding the roots of quadratic equation learned in Schools? No? Don't worry. Before solving this problem I also couldn't remember. xD

Here I am not going to discuss how to find the roots of quadratic equation by the actual mathematical process, yeah that's not my job and I am also bad at Maths, but I will show you how to implement the same thing in C Programming. If you have forgot the process of finding the roots then you can revise that part from this link and this. Here the process is explained well which I think any mid-school kid can even understand xD.

Find Roots of Quadratic Equation in C Programming


So let's first see the code to find the roots of quadratic equation in C Programming.

#include <stdio.h>
#include <conio.h>
#include <math.h>
 
int main()
{
   int a, b, c, d;
   double root1, root2;
 
   printf("Enter a, b and c where a*x*x + b*x + c = 0\n");
   scanf("%d%d%d", &a, &b, &c);
 
   d = b*b - 4*a*c;
 
   if (d < 0) { //complex roots
     printf("First root = %.2lf + j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
     printf("Second root = %.2lf - j%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
   }
   else { //real roots
      root1 = (-b + sqrt(d))/(2*a);
      root2 = (-b - sqrt(d))/(2*a);
 
      printf("First root = %.2lf\n", root1);
      printf("Second root = %.2lf\n", root2);
   }
   getch();
   return 0;
}

Here we declare five variable of integer and double type. First three are to hold the co-efficient of the variables and later two to hold the roots. Then we find the discriminant with the help of the formula b*b-4*a*c.

Next we make two test conditions. If d is less than 0 then we output the complex Roots with 'i' as iota. There is nothing about programming here.. all the formulas I have used are all from normal mathematics. The important part is, we have used sqrt() function here to find out the root of a number which is available in math.h header file. So you have to add the header file at the top of the program to actually use this function.In the else part where d>0 we output the roots by the normal formulas.

I hope you have understood the concept as well as the code well.If you have any doubt or question about the code then you can ask me in the comments. Thanks :)    
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
© 2011 Standard Programming ∙ Designed by BlogThietKe
Released under Creative Commons 3.0 CC BY-NC 3.0