Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Find roots of quadratic equation for both real and imaginary roots

0 votes
asked Apr 29, 2020 by Devdeep verma (120 points)

2 Answers

0 votes
answered May 2, 2020 by Abhinav (150 points)

#include<stdio.h>
#include<math.h>
int main ()
{
  float a,b,c,P,D,i;

   printf("\t The Quadratic Equation looks like this : (a)x*x + (b)x + c = 0\nYou will be entered the value of a,b and c");
  printf("\nEnter the Values of a,b,c\n");
  printf("Quadratic Equation is : a(x*x)+b(x)+c=0\n");
  printf("Here 'a' is : Leading Coefficient of a Quadratic Equation OR coeff. of x*x\n");
  printf("Here 'b' is : Coefficient of x\n");
  printf("Here 'c' is : Absolute Constant\n");
  printf("\n    Enter the value of a  :  ");
  scanf("%f",&a);
  printf("    Enter the value of b  :  ");
  scanf("%f",&b);
  printf("    Enter the value of c  :  ");
  scanf("%f",&c);
  D=(b*b-4*a*c);
  P=sqrt(D);
  if (a==0)
  {
      printf("\n     Warning ! For Quadratic Equation a>0 and a<0");
  }

  else
  {
    if (D<0)
  {
      printf("\n                Roots are Imaginary\n");
  printf("\n  In the Complex Form (p+iq) and (p-iq)\n");
  printf("  Where 'i' is iota: value is square root of -1\n-------\n");
  i=sqrt(-D);
  printf("  First Root : %.2f + %.2f i\n",(-b)/(2*a),i/(2*a));
  printf("  Second Root: %.2f - %.2f i\n-------\n",(-b)/(2*a),i/(2*a));
  }
  else if (D==0)
  {
      printf("                \n              Roots are equal , perfect Square\n");
      printf("  \n   First Root  :    %.2f\n",((-b)+P)/(2*a));
      printf("   Second Root :    %.2f\n",((-b)-P)/(2*a));
  }
  else if (D>0)
{
    printf("             \n                  Roots are Real and Different\n");
  printf("   \n   First Root             :   %.2f     \n",((-b)+P)/(2*a));
  printf("   Second Root           :   %.2f     \n---------\n",((-b)-P)/(2*a));
  printf("   Discriminant           :   %.2f     \n---------\n",D);
  printf("   Sum Of roots           :   %.2f     \n", -b/a);
  printf("   Product of roots       :   %.2f     \n", c/a);
  printf("   Difference of roots    :   %.2f     \n", P/a);
}
  }
  return 0;
}

+1 vote
answered May 3, 2020 by dinesh sen (290 points)
#I have written this programme in python, please let me know if there is any mistake.

#solve quadritic equation ax2+bx+c=0
print("This is for solving quadritic equation ax2+bx+c=0")
a=input('Enter the value of a : \n')
b=input('Enter the value of b : \n')
c=input('Enter the value of c : \n')

a=int(a)
b=int(b)
c=int(c)

#Now we have to solve the equation the formula is
#x=-b+root(b2-4ac)/2a and
#x=-b-root(b2-4ac)/2a
if ((b*b-4*a*c)<0):
    print('No real number solution exist')
else:
    solution1=(-b+(b*b-4*a*c)**0.5)/(2*a)
    solution2=(-b-(b*b-4*a*c)**0.5)/(2*a)
    print('Two solutions for the given equation are {} and {}'.format(solution1,solution2))
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...