Skip to content

Instantly share code, notes, and snippets.

@kala13x
Created October 22, 2015 13:41
Show Gist options
  • Select an option

  • Save kala13x/1fefdc3fa828d735eea0 to your computer and use it in GitHub Desktop.

Select an option

Save kala13x/1fefdc3fa828d735eea0 to your computer and use it in GitHub Desktop.
Simple app to solve quadratic equation
/*
* quad-equatin.c
*
* Copyleft (C) 2013 Sun Dro
*
* Simple app to solve quadratic equation
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Simple math macros */
#define D(a,b,c) ((b<0)?((b*b)+(4*a*c)):(b*b)-(4*a*c))
#define X1(a,b,c) ((-b+sqrt(D(a,b,c)))/(2*a))
#define X2(a,b,c) ((-b-sqrt(D(a,b,c)))/(2*a))
int main(int argc, char *argv[])
{
/* Check arguments */
if (argc < 4)
{
printf("usage: %s [a] [b] [c]\n", argv[0]);
printf("example: %s 1 4 4\n", argv[0]);
return 0;
}
/* Get arguments */
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c = atoi(argv[3]);
/* Get the x-es */
if (D(a,b,c) < 0) printf("There is no X\n");
else printf("X1 = %3.1f, X2 = %3.1f\n", X1(a,b,c), X2(a,b,c));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment