Saturday, October 26, 2013

Quatratic Equation Using C

QUADRATIC EQUATION USING C

Program:
#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);
quad(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

No comments: