1.07.2012

Quadratic eq. roots

Q. Write a program to find the roots of a quadratic equation.

Ans.


Process of finding root of quadratic equation in C program:
The roots of any quadratic equation of the the form,
 
ax2+bx+c=0
has minimum two roots.
The formula of calculating these roots are as:



Where:r1,r2 are roots. First of all user entered values of a,b and c. Then it finds out numerator- which is the discriminate. Then checks if the numerator is greater, equals or less than zero. Prints out the results according to the situation.


/*program to find the root of a quadratic equation*/
#include<stdio.h>

#include<conio.h>
#include<math.h>
int main()
{
  int a,b,c;
  float r1,r2,up;
  printf("Enter value of a : ");
  scanf("%d", &a);
  printf("Enter value of b : ");
  scanf("%d", &b);
  printf("Enter value of c : ");
  scanf("%d", &c);
  up=(b*b)-(4*a*c);
  if(up>0)

 {
    printf("\n ROOTS ARE REAL ROOTS\n");
    r1 = ((-b) + sqrt(up)) /(2*a);
    r2 = ((-b) - sqrt(up)) /(2*a);
    printf("\n ROOTS : %f, %f\n", r1, r2);
  }
  else if(up==0)
  {
    printf("\n ROOTS ARE EQUAL\n");
    r1 = (-b/(2*a));
    printf("\n ROOT IS...: %f\n", r1);
  }
  else
     printf("\n ROOTS ARE IMAGINARY ROOTS\n");
  getch();

 return 0;
}

Output:-
 

Enter value of a : 6
Enter value of b : -13
Enter value of c : 6
ROOTS ARE REAL ROOTS
ROOTS : 1.500000, 0.666667



Enter value of a : 1
Enter value of b : 1
Enter value of c : -2
ROOTS ARE REAL ROOTS
ROOTS : 1.000000, -2.000000

1 comment:

  1. i want this program in the form of algorithm and flowchart

    ReplyDelete