• Log in or sign up for an account here: https://www.sythe.org/login

Quadratic formula solver in C

Govind

The One Musketeer

Highly Respected
Mudkips
Retired Administrator
Joined
Apr 22, 2005
Sythe Gold
23
Messages
7,825
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
	double a, b, c, x1, x2;
	a = 0; b = 0; c = 0;
	if(!(argv[1])||(argv[1][0]=='/'&&argv[1][1]=='?'))
	{
		printf("QUAD A B C\nTo solve quadratics in the form 

Ax^2+Bx+C=0");
		return 0;
	}
	if(argc!=4)
	{
		printf("Usage error: Must be QUAD a b c for any quadratic 

ax^2+bx+c=0");
		return 0;
	}
	a = strtod(argv[1],0);
	b = strtod(argv[2],0);
	c = strtod(argv[3],0);
	if(a==0)
	{
		printf("A cannot be 0");
		return 0;
	}
	//printf("%f %f %f\n",a,b,c);
	if(((b*b)-(4*a*c))<0)
	{
		printf("Nonreal roots\n");
		return 0;
	}
	x1 = (-b+sqrt((b*b)-(4*a*c)))/(2*a);
	x2 = (-b-sqrt((b*b)-(4*a*c)))/(2*a);
	printf("x[1] = %f\n",x1);
	printf("x[2] = %f\n",x2);
	return 0;
}

I know that the checking for whether argv[1] = "/?" was a little crude but other than that it's awesome, I think.
 
Runescape Gamblingrunescape gambling
Back
Top