Code Scrappers


Quadratic Equation - C program


                    #include<stdio.h>
                    #include<stdlib.h>
                    #include<math.h>
                    void main()
                    {
                        float a, b, c, d;

                        float root1, root2, real, imaginary;

                        printf("\nEnter the coefficients x^2 : ");

                        scanf("%f",&a);

                        printf("\nEnter the coefficient of x : ");

                        scanf("%f",&b);

                        printf("\n Enter the constant term : ");

                        scanf("%f",&c);

                        d = b*b-4*a*c;

                        if (d > 0)
                        {

                            root1 = (-b+sqrt(d))/(2*a);

                            root2 = (-b-sqrt(d))/(2*a);

                            printf("root1 = %f and root2 = %f",root1 ,root2);
                        }

                        else if (d == 0)
                        {
                            root1 = root2 = -b/(2*a);

                            printf("root1 = root2 = %f;", root1);

                        }

                        else
                        {
                            real = -b/(2*a);

                            imaginary = sqrt(-d)/(2*a);

                            printf("root1 = %f + %fi and root2 = %f - %fi", real, imaginary, real, imaginary);
                        }

                    }
                



Output


      Enter the coefficients x^2 : 1

      Enter the coefficient of x : 2

      Enter the constant term : 1

      root1 = root2 = -1.000000;