Follow This Blog For more... 😊

Program to find the roots of a quadratic equation.| Using C language| C Program series 12|

👨‍💻Program to find the roots of a quadratic equation.

Code:


    //Program to find the roots of a quadratic equation.
   
    #include<stdio.h>
    #include<math.h>
    main()
    {
        printf("Program to find the roots of a quadratic equation.\n");
        printf("NOTE:equation like a(x^2) + b(x) + c = 0 Where a,b,c are coeffiecients.\n\n");
       
        float a,b,c,D,R1,R2;
        /*
        Where,
        a = Coefficient of x^2
        b = Coefficient of x
        c = Constant
        D = Discriminant
        R1= First Root
        R2= Second Root
        */
        printf("Enter value of a:");
        scanf("%f",&a);
        printf("Enter value of b:");
        scanf("%f",&b);
        printf("Enter value of c:");
        scanf("%f",&c);
       
        D=(b*b)-(4*a*c);
       
        if(D>=0)
        {
            R1=((-1*b)+(sqrt(D)))/(2*a);
            R2=((-1*b)-(sqrt(D)))/(2*a);
            printf("\nRoots are Real.\n");
            printf("R1 = %f \nR2 = %f\n",R1,R2);
        }
        else
        {
            printf("\nRoots are imeginary.\n");
            printf("R1= ((%f) + i(%f)^(1/2)) / (%f)\n\n",-1*b,-1*D,2*a);
            printf("R2= ((%f) - i(%f)^(1/2)) / (%f)\n",-1*b,-1*D,2*a);
            //Here,'i' represents iota, where i = (square root of -1)
        }
    }
     

Output:




👉Click for more C Program series Questions.[Questions list]

Comments

Popular Posts