Wednesday 12 September 2012

To find the roots of the quadratic equation


Description:
Nature of roots of quadratic equation can be known from the quadrant ê = b2-4ac
If  b2-4ac >0 then roots are real and unequal
If  b2-4ac =0 then roots are real and equal
If  b2-4ac <0 then roots are imaginary
Algorithm:
Step 1: start
Step 2: read the a,b,c value
Step 3: if (b*b-4ac)>0 then
                        Root 1= (-b+ pow((b*b-4*a*c),0.5))/2*a
Root 2= (-b-pow((b*b-4*a*c),0.5))/2*a
Step 4: if (b*b-4ac)=0 then
                        Root1 = Root2 = -b/(2*a)
Step 5: Otherwise Print Imaginary roots. Goto step 7.
Step 6: print roots
Step 7: stop
 
Program:
 #include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,d;
clrscr();
printf("Enter the values for equation:");
scanf("%f%f%f",&a,&b,&c);
/* check the condition */
if(a==0)
printf("Enter value should not be zero ");
else
{
             d=b*b-4*a*c;
/* check the condition */
  if(d>0)
            {
                        r1=(-b+sqrt(d)/(2*a));
                         r2=(-b-sqrt(d)/(2*a));
                         printf("roots are real and unequal\n");
                         printf("%f\n%f\n",r1,r2);
             }
  else
  if(d==0)
            {
                        r1=-b/(2*a);
                         r2=-b/(2*a);
                         printf("roots are real and equal\n");
                         printf("root=%f\n",r1);
                          printf("root=%f\n",r2);
  }
             else
                         printf("roots are imaginary");
    }
    getch();
}
Output:
1. Enter the values for equation: 1, 6, 9
    Roots are real and equal
    Root= -3.0000
     Root= -3.0000
2. Enter the values for equation: 2, 7, 6
     Roots are real and unequal
    Root= -6.75
    Root= -7.25
3. Enter the values for equation: 1, 2, 3
    Roots are imaginary
Conclusion: The program is error free




VIVA QUESATIONS:

1) What are various types of loop statements?
Ans :    While, do- while, for loop statements

4)      What is the difference between while and do-while statements?
Ans:     In while the condition will be checked first and then enter into a loop.
             But in do- while the statements will be executed first and then finally check the 
             Condition.
3)   How to find the roots of qudratric equtations ?
Ans:  Nature of roots of quadratic equation can be known from the quadrant
          ê =    b2-4ac
If  b2-4ac >0 then roots are real and unequal
If  b2-4ac =0 then roots are real and equal
            If  b2-4ac <0 then roots are imaginary

4) List out the C features ?
Ans: Portability,flexibility, wide acceptability etc..,

No comments:

Post a Comment