# Quadratic - calculate discriminant d = b*b 4*a*c if(d< 0 printf"The quadratic has no real roots\n" else if(b>= 0 q =-b sqrt(d x1 =

This preview shows page 1. Sign up to view the full content.

/* quadratic.c * Code to determine roots of a quadratic equation. */ #include <stdio.h> #include <math.h> int main() { float a, b, c, d, q, x1, x2; /* prompt user for quadratic coefficients */ printf("Quadratic coefficient a? "); scanf("%f", &a); printf("Quadratic coefficient b? "); scanf("%f", &b); printf("Quadratic coefficient c? "); scanf("%f", &c);
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: /* calculate discriminant */ d = b*b - 4*a*c; if (d < 0) { printf("The quadratic has no real roots\n"); } else { if (b >= 0) { q = -b - sqrt(d); x1 = 2.*c/q; x2 = q/(2.*a); } else { q = -b + sqrt(d); x1 = q/(2.*a); x2 = 2.*c/q; } printf("The two roots are x1 = %f, x2 = %f\n", x1, x2); } return 0; }...
View Full Document

## This note was uploaded on 09/29/2009 for the course COSC 1002 taught by Professor Wheatland during the Three '09 term at University of Sydney.

Ask a homework question - tutors are online