-->
Showing posts with label program to find roots/values of quadratic equation. Show all posts
Showing posts with label program to find roots/values of quadratic equation. Show all posts

a program to calculate roots of quadratic equation (ax2+bx+c).

//calculate roots of quadratic equation (ax2+bx+c). Conditions to be taken into account are
// (b2-4ac)>0 and 2a>0
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c;
float x1,x2;
float root_value;
printf("enter value for a,b and c\n");
scanf("%f%f%f",&a,&b,&c);
root_value=pow(b,2)-4*a*c;
if(root_value>0 && a>0)
{
printf("roots are possible\n");
x1=(-b+sqrt(root_value))/2*a;
x2=(-b+sqrt(root_value))/2*a;
printf("first root/value=%f\n",x1);
printf("second root/value=%f",x2);
}
else
{
printf("real roots are not possible\n instead,");
printf("imaginary roots are possible");
}
getch();
}                                                                                                                 
                                                                                                                       

program to get values in of x in quadratic equation

//program to get values of x in quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2;
printf("enter values of a,b and c\n");
scanf("%f%f%f",&a,&b,&c);
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("the first value=%f\n",x1);
printf("second value is=%f",x2);
getch();
}

note:

according to formula the condition should be satisfied.like
1) (b2-4ac)>0 i.e. b2>4ac
2)a>0 for 2a
otherwise the program terminates abnormally without any result.
later we will see this program with 'if' structure'.

Logically that is said to be right.