This blog is under construction

Sunday 23 June 2013

C program to find the roots of a quadratic equation

Write a C program to calculate the roots of a quadratic equation.


  #include <stdio.h>
  #include <math.h>
  int main() {
        int a, b, c, temp;
        float proot, nroot;
        printf("aX^2 - bX + c\n");
        printf("Enter your value for a, b and c:");
        /* get the input from user */
        scanf("%d%d%d", &a, &b, &c);

        temp  = (b * b) - (4 * a * c);
        if (temp < 0) {
                printf("Imaginary root\n");
                return 0;
        }

        /* positive root calculation */
        proot = (-b + sqrt(temp)) / (2.0 * a);

        /* negative root calculation */
        nroot = (-b - sqrt(temp)) / (2.0 * a);

        /* result */
        printf("roots of the given quadratic equations:\n");
        printf("%.2f  %.2f\n", proot, nroot);
        return 0;
  }


Note: gcc roots.c -lm => link math library for sqrt() function.

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  aX^2 - bX + c
  Enter your value for a, b and c:1 -4  4
  roots of the given quadratic equations:
  2.00  2.00





See Also:

No comments:

Post a Comment