uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

quadratic_equation.c (993B)


      1 #include <stdio.h>
      2 #include <math.h>
      3 
      4 
      5 void linearEquation();
      6 void quadraticEquation();
      7 
      8 
      9 int main(int argc, char **argv)
     10 {
     11     double a, b, c; 
     12 
     13     printf("a: ");
     14     scanf("%lf", &a);
     15     printf("b: ");
     16     scanf("%lf", &b);
     17     printf("c: ");
     18     scanf("%lf", &c);
     19 
     20     if (a != 0)
     21         quadraticEquation(a, b, c);
     22     else
     23         linearEquation(b, c);
     24     
     25     return 0;
     26 }
     27 
     28 
     29 void linearEquation(double b, double c)
     30 {
     31     double x;
     32 
     33     if (b != 0)
     34     {
     35         x = -c / b;
     36         printf("x = %.2lf\n", x);
     37     }
     38     else
     39         printf("Infinite solutions.\n");
     40 }
     41 
     42 
     43 void quadraticEquation(double a, double b, double c)
     44 {
     45     double x, x1, x2, D = pow(b, 2) - 4*(a*c);
     46 
     47     if (D > 0)
     48     {
     49         x1 = (-b + sqrt(D)) / (2*a);
     50         x2 = (-b - sqrt(D)) / (2*a);
     51         printf("x1 = %.2lf\nx2 = %.2lf\n", x1, x2);
     52     }
     53     else if (D == 0)
     54     {
     55         x = (-b) / (2*a);
     56         printf("x = %.2lf\n", x);
     57     }
     58     else
     59     {
     60         printf("There are no solutions.\n");
     61     }
     62 }