3. C PROGRAMMING LAB | Check Now

C PROGRAMMING LAB –03] Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print the appropriate messages.


Algorithm

  • Step-1: [Read values of A, B, C]-Read A, B, C.
  • Step-2: [Check for Co-efficient]-If A=B=C=0 then roots are non-determinant
  • Step-3:[Compute the value Disc]- Disc=B*B-4*A*C;
  • Step-4:[Different roots are obtained depending on the value of disc]-
    • If Disc is lesser than 0 than goto step 5
    • If Disc is equals to 0 than goto step 6
    • If Disc is greater than 0 than goto step 7.
  • Step-5: Print Imaginary roots.
    • Realp=-B/2*A;
    • Imagp=sqrt(Disc)/2*A;
    • Root1=realp + imagep;
    • Root2 = realp – imagep;
  • Step-6:Roots are real and equal
    • Root1=B/2*A;
    • Root2 = root1;
  • Step-7: roots are real and distinct
    • Root1= – B +sqrt(Disc)/2*A;
    • Root2 = -B -sqrt(Disc)/2*A;
  • Step-8: Stop
See also  9. C PROGRAMMING LAB | Check Now

Flow Chart

C PROGRAMMING - roots of quadratic equation flow chart

Program-3 Source Code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void main()
{
 float a,b,c,disc;
 float root1,root2,realp,imagp;
 printf("Enter values of a,b,c\n");
 scanf("%f%f%f",&a,&b,&c);

 if(a==0 && b==0 && c==0)
 {
  printf("roots cannot be determined\n");
  exit(0);
 }
 else
 {
  disc=b*b-4*a*c;
  if(disc>0)
  {
   root1=(-b+sqrt(disc))/(2*a);
   root2=(-b-sqrt(disc))/(2*a);
   printf("roots are real and distinct\n");
   printf("root1=%f\n",root1);
   printf("root2=%f\n",root2);
  }

  else if(disc==0)
  {
   root1=-b/(2*a);
   root2=root1;
   printf("roots are real and equal\n");
   printf("root1=%f\n",root1);
   printf("root2=%f\n",root2);
  }
  
  else if(disc<0)
  {
   realp=-b/(2*a);
   imagp=sqrt(abs(disc)/(2*a));
   printf("roots are complex\n");
   printf("root1=%f+i%f\n",realp,imagp);
   printf("root2=%f-i%f\n",realp,imagp);
  }
 }

}

C PROGRAMMING -Output

  • Enter the values of a, b, c
    • 1
    • 2
    • 1
    • roots are real and equal
    • root1=-1.000000
    • root2=-1.000000
  • Enter the values of a, b, c
    • 2.3
    • 4
    • 5.6
    • roots are complex
    • roots1=-0.869565 + i2.758386
    • roots2=-0.869565 – i2.758386
  • Enter values of a, b, c
    • 0
    • 0
    • 0
    • roots cannot be determined
See also  5. AI AND MACHINE LEARNING VTU LAB | READ NOW

C PROGRAMMING -Viva Questions

1] What is a quadratic equation?

2] What is math.h? why it is used in the C program?

3] What are decision-making statements in C language?

4] Write the syntax of the “if” statement?

5]Difference between if and switch statements?

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp Icon Join For Job Alerts