Skip to content

Instantly share code, notes, and snippets.

@mehmeteminkartal
Created October 31, 2017 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehmeteminkartal/56a15f62a2e64be700b1baf64059cd24 to your computer and use it in GitHub Desktop.
Save mehmeteminkartal/56a15f62a2e64be700b1baf64059cd24 to your computer and use it in GitHub Desktop.
Finds the roots for given quadratic equation values
#include "findroots.h"
void calcroots(int a, int b, int c) {
printf("%dx^2 + %dx + %d = 0\n", a, b, c);
int delta = pow(b, 2) - (4 * a * c);
printf("Δ = %d\n", delta);
double sqrtdelta = sqrt((double)(delta));
if (delta < 0) {
printf("There are no real roots for this equation!\n");
} else if (delta > 0) {
double root1 = ((double)(-b) + sqrtdelta) / (2.0 * (double)a);
double root2 = ((double)(-b) - sqrtdelta) / (2.0 * (double)a);
printf("Root1: %lf\n", root1);
printf("Root2: %lf\n", root2);
} else { // delta == 0
double root = (-(double)b) / (2.0 * (double)a);
printf("Root: %lf\n", root);
}
}
#pragma once
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
/**
Finds the roots for given quadratic equation values
@param a X^2 value
@param b X value
@param c constant
*/
void calcroots(int, int, int);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment