WRITE A C PROGRAM TO SOLVE QUADRATIC EQUATION - Computer Programming

# Computer Programming

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

## Sunday, October 30, 2011

WRITE A PROGRAM TO SOLVE QUADRATIC EQUATION

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
void quad(int a, int b ,int c);
int a,b,c;
float r;
clrscr();
printf(“Enter values of a,b,c of a quadratic equation:\n” );
scanf(“%d%d%d”,&a,&b,&c);
getch();
}

void quad(int a, int b ,int c)
{
float d,r1,r2,r3,r4,r;
d=(b*b)-(4*a*c);
switch(d)
{
case <0:
printf(“Value of Discriminant is negative” );
break;
case 0:
printf(“ Roots are real” );
r=-b/2*a;
printf(“ First and Second root of equation:%d,r” );
break;
default:
r1=-b+sqrt(d);
r2=2*a;
r=r1/r2;
r3=-b-sqrt(d);
r4=r3/r2;
break;
printf(“First root of equation: %d”,r);
printf(“/n Second root of equation: %d”,r4);
}
}

OUTPUT:

Enter values of a,b,c of a quadratic equation: 2
2
4

Value of Discriminant is negative

Enter values of a,b,c of a quadratic equation: 1
5
2

First root of equation: -2.9384
Second root of equation: -7.0615

Enter values of a,b,c of a quadratic equation: 2
4
2
Roots are real
First and Second root of equation: -1