Search This Blog

Thursday, October 13, 2011

Write a function to get, as arguments, co-efficient of quadratic equations ax2+bx+c=0 and evaluate and display root

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  #include<stdlib.h>
   5:  void showroots(float a,float b,float c);
   6:  void main()
   7:  {
   8:  float a,b,c;
   9:  clrscr();
  10:  printf("\nEnter co-efficient of a,b and c: ");
  11:  scanf("%f%f%f",&a,&b,&c);
  12:  showroots(a,b,c);
  13:  getch();
  14:  }
  15:   
  16:  void showroots(float a,float b,float c)
  17:  {
  18:  float delta,root1,root2;
  19:  if(a==0)
  20:  {
  21:  printf("\nco-efficent of a cant be 0");
  22:  exit(0);
  23:  }
  24:  delta=b*b-4.0*a*c;
  25:  if(delta<0)
  26:  {
  27:  printf("\nThe equation has no real roots");
  28:  exit(0);
  29:  }
  30:  root1=(-b+sqrt(delta))/(2*a);
  31:  root2=(-b-sqrt(delta))/(2*a);
  32:  printf("\nroot1=%f\nroot2=%f",root1,root2);
  33:  }

No comments:

Post a Comment