Program to find all roots of a quadratic equation in C

Source Code

// 12. Program to find all roots of a quadratic equation
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
//#include <conio.h>
#include <math.h>
//#include <stdlib.h>

void main () {
	float x, y, a, b, c;
	//clrscr();
	//system("cls");
	
	printf("\t\t..:: find all roots of a quadratic equation ::..\n\n");
/*	printf("Enter coefficient of x's square: ");
	scanf("%f", &a);
	printf("Enter coefficient of x: ");
	scanf("%f", &b);
	printf("Enter the constant term: ");
	scanf("%f", &c);*/
	
	printf("Please enter values for coefficient of x's square,\
 coefficient of x and constant term respectively: ");
 	scanf("%f%f%f", &a, &b, &c);
 	
	x = (-b + sqrt(b*b-4*a*c))/2*a;
	y = (-b - sqrt(b*b-4*a*c))/2*a;
	
	printf("The two roots are: %f and %f", x, y);
	
	/*if (b*b-4*a*c >= 0) {
		x = (-b + sqrt(b*b-4*a*c))/2*a;
		y = (-b - sqrt(b*b-4*a*c))/2*a;
		printf("The two roots are: %f and %f", x, y);
	} else {
		x = (-b + sqrt(4*a*c-b*b))/2*a;
		y = (-b - sqrt(4*a*c-b*b))/2*a;
		printf("The two roots are: %f and %f", x, y);
	}*/
	
	//getch();
}

Leave a Reply

Note: Your email address will not be published. Required fields are marked *