Advertisement
HeekoOfficial

ABC Formula in C

Feb 21st, 2020
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. /*Schrijf een programma, ontwerp en code, waarmee de wortels van een willekeurig
  2. tweede-graad-vergelijking te vinden zijn. Zoals bekend is uit de analyse, zijn deze
  3. wortels te berekenen met de ABC – formule:
  4.                     (𝑥1,2 = ) −𝑏∓√𝑏^2−4𝑎𝑐 / 2𝑎
  5. Indien de discriminant (het deel onder de wortel) negatief is, zijn er geen reële
  6. oplossingen maar zijn er twee imaginaire oplossingen. Indien de discriminant nul is,
  7. is er één oplossing. En als de discriminant positief is, zijn er twee reële oplossingen.
  8.  
  9.  In het eerste geval wordt de mededeling “discriminant is negatief” afgedrukt. Voor
  10. de andere gevallen worden de oplossingen afgedrukt.
  11. Het programma moet tenminste de volgende functies bevatten:
  12.  void abc(double a, double b, double c)
  13.  double discriminant(double a, double b, double c)*/
  14.  
  15. ------------------------------------------------------Written in dutch-----------------------------------------------------------------
  16.  
  17. #include <stdio.h>
  18. #include <math.h>
  19. double discriminant(double a, double b, double c)
  20. {
  21.     return (pow(b, 2) - 4 * a * c);
  22. }
  23. void abc(double a, double b, double c)
  24. {
  25.     if(discriminant(a, b, c) < 0){
  26.         printf("De vergelijking %.4lfx^2 + %.4lfx^2 + %.4lfx^2 heeft geen reele oplossingen.\n", a, b, c);
  27.         return;
  28.     }
  29.     double formula1 = (-b + sqrt(discriminant(a, b, c))) / (2 * a);
  30.     if(discriminant(a, b, c) == 0){
  31.         printf("De oplossing van %.4lfx^2 + %.4lfx^2 + %.4lfx^2 is:\nx = %.4lf\n", a, b, c, formula1);
  32.         return;
  33.     }
  34.     double formula2 = (-b - sqrt(discriminant(a, b, c))) / (2 * a);
  35.     if(discriminant(a, b, c) > 0){
  36.         printf("De oplossingen van %.4lfx^2 + %.4lfx^2 + %.4lfx^2 is:\n(x1 = %.4lf), (x2 = %.4lf)\n", a, b, c, formula1, formula2);
  37.     }
  38. }
  39. int main() {
  40.     int testcases;
  41.     scanf("%d",&testcases);
  42.     for(int i=0; i < testcases; i++ ){
  43.         double a, b, c;// doubling it
  44.         scanf("%lf%lf%lf", &a, &b, &c);//has double value/ user input
  45.         abc(a, b, c);// send user input to abc
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement