Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, November 30, 2014

Program to Solve Quadratic Equation Using Conditional operator and Switch in C.






Code Box
-------------------------------------------

/* Program to Solve Quadratic Equation Using Conditional operator and Switch */

/* In quadratic equation we need values of a,b,c
and D=(b square -4.a.c)
and every thing depends on the value of D
*If D grater than 0 multiple and real root
*If D==0 only single root
*If D is less than 0 than imaginary roots

*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,D,n;

float x1,x2,D1;

clrscr();

printf("\nEnter Values");
printf("\n a = ");
scanf("%d",&a);

printf("\n b = ");
scanf("%d",&b);

printf("\n c = ");
scanf("%d",&c);

printf("\nEnterd Values are : a=%d b=%d c=%d",a,b,c);

printf("\nQuadratic Equation Calculation:");

D=((b*b)-(4*a*c));

printf("\nValue of Normal D= %d ",D);
D1=abs(D);
D1=sqrt(D1);

printf("\nValue of Squared D= %.3f ",D1);


n=(D>0)?((D==0)?1:2):3;


switch(n)
{
case 1:

//Multiple Roots
printf("\nReal & Multiple Roots");

x1=((b+D1)/(2*a));

x2=((b-D1)/(2*a));

printf("\n\nRoots are X1=%.3f X2=%.3f ",x1,x2);

break;

case 2:

//Single Root
printf("\nReal & Single Roots");

x1=((b)/(2*a));

printf("\n\nRoots are X1=%.3f",x1);


break;

case 3: //Imaginary Part

printf("\nImaginary Roots");

printf("\nX1=(%d+%.3fi)/2*%d",b,D1,a);

printf("\nX2=(%d-%.3fi)/2*%d",b,D1,a);


break;

default:

printf("\nWrong Entry, Error!!!");
}

getch();

No comments: