program to find the roots of a quadratic equation
Answers were Sorted based on User's Feedback
Answer / dhananjay
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
float a,b,c,x1,x2,disc;
clrscr();
printf("Enter the co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;/*to find discriminant*/
if(disc>0)/*distinct roots*/
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("The roots are distinct\n");
exit(0);
}
if(disc==0)/*Equal roots*/
{
x1=x2=-b/(2*a);
printf("The roots are equal\n");
printf("x1=%f\nx2=%f\n",x1,x2);
exit(0);
}
x1=-b/(2*a);/*complex roots*/
x2=sqrt(fabs(disc))/(2*a);
printf("The roots are complex\n");
printf("The first root=%f+i%f\n",x1,x2);
printf("The second root=%f-i%f\n",x1,x2);
getch();
}
Is This Answer Correct ? | 325 Yes | 117 No |
Answer / pratibha
//program to find roots of given equation
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,p,q,r,s;
printf("\n\n Enter the values of a,b,c")
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d=0)
{
printf("\n The roots are real and equal");
p=(-b)/(2*a);
q=(-b)/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d>0)
{
printf("\n The roots are real and distinct");
p=((-b)+sqrt((b*b)-(4*a*c)))/(2*a);
q=((-b)-sqrt((b*b)-(4*a*c)))/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d<0)
{
printf("\n The roots are imaginary");
r=(-b)/(2*a);
s=(sqrt((4*a*c)-(b*b)))/(2*a);
printf("\n The roots of the equation are
p=%f=%fi,q=%f-%fi",r,s,r,s);
}
}
Is This Answer Correct ? | 110 Yes | 41 No |
Answer / vikas tiwari
#include <iostream.h>
#include <conio.h>
#include <math.h>
int main()
{
clrscr();
float a,b,c,d,root1,root2;
cout << "Enter the 3 coefficients a, b, c : " << endl;
cin>>a>>b>>c;
if(!a){
if(!b)
cout << "Both a and b cannot be 0 in ax^2 + bx + c = 0" << "\n";
else
{
d=-c/b;
cout << "The solution of the linear equation is : " << d << endl;
}
}
else
{
d=b*b-4*a*c;
if(d>0)
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
cout << "The first root = " << root1 << endl;
cout << "The second root = " << root2 << endl;
}
getch();
return 0;
}
Is This Answer Correct ? | 32 Yes | 20 No |
Answer / lady k
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main ()
{
/*Main: Solve the quadratic equation*/
float r, a, b, c, x1, x2, d;
printf ("Enter the value of a: ");
scanf ("%f", &a);
printf ("\nEnter the value of b: ");
scanf ("%f", &b);
printf ("\nEnter the value of c: ");
scanf ("%f", &c);
printf("\n");
d= b*b-4*a*c;
r= sqrt(d);
x1= (-b + r)/(2*a);
x2=(-b- r)/(2*a);
if (d>0)
{
printf ("The real roots of the equation are %f",
x1);
printf (" and %f", x2);
}
else if (x1==x2)
{
x1= (-b+r)/(2*a);
printf ("x1 and x2 are equal roots %f", x1);
}
else if (a<0)
{
x1= (-b + r)/(2*a);
x2=(-b- r)/(2*a);
printf ("The roots of the equation are %f", x1);
printf (" and %f", x2);
}
else
printf ("The roots of the equation are
complex");
getch ();
return 0;
}
Is This Answer Correct ? | 12 Yes | 6 No |
Answer / bawaaaa
//program to find roots of given equation
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,p,q,r,s;
printf("\n\n Enter the values of a,b,c")
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d=0)
{
printf("\n The roots are real and equal");
p=(-b)/(2*a);
q=(-b)/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d>0)
{
printf("\n The roots are real and distinct");
p=((-b)+sqrt((b*b)-(4*a*c)))/(2*a);
q=((-b)-sqrt((b*b)-(4*a*c)))/(2*a);
printf("\n The roots of the equation are %f,%f",p,q);
}
if(d<0)
{
printf("\n The roots are imaginary");
r=(-b)/(2*a);
s=(sqrt((4*a*c)-(b*b)))/(2*a);
printf("\n The roots of the equation are
p=%f=%fi,q=%f-%fi",r,s,r,s);
}
}
Is This Answer Correct ? | 32 Yes | 28 No |
Answer / asif ahmed
/* PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION WITH
APPROPRIATE ERROR MESSAGE */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,disc,root1,root2,part_r,part_i;
int flag;
clrscr();
printf("Enter the coefficients of quadratic equation \n");
scanf("%f%f%f",&a,&b,&c);
printf("coefficients are a=%f\t b=%f\t c=%f\n",a,b,c);
if((a==0)||(b==0)||(c==0))
printf("\nZero coefficeints");
else
{
disc=(b*b)-(4*a*c);
if(disc==0)
flag=0;
if(disc>0)
flag=1;
if(disc<0)
flag=2;
switch(flag)
{
case0 : printf("\n Roots are real and equal");
root1=-(-b)/(2*a);
root2=root1;
printf("\n root1=%f",root1);
printf("\n root2=%f",root2);
break;
case1 : printf("\n Roots are real and distinct");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("\n root1=%f",root1);
printf("\n root2=%f",root2);
break;
case2 : printf("\n Roots are real and Imaginary");
part_r=(-b)/(2*a);
part_i=(sqrt(-disc))/(2.0*a);
printf("\n root1=%f +i%f",part_r,part_i);
printf("\n root2=%f -i%f",part_r,part_i);
break;
}
}
getch();
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / rupamjit
//program to find roots of given equation
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,p,q,r,s;
printf("\n\n Enter the values of a,b,c=");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0)
{
printf("\n The roots are real and equal.");
p=(-b)/(2*a);
q=(-b)/(2*a);
printf("\n The roots of the equation are %.0fand%.0f",p,q);
}
if(d>0)
{
printf("\n The roots are real and distinct");
p=((-b)+sqrt((b*b)-(4*a*c)))/(2*a);
q=((-b)-sqrt((b*b)-(4*a*c)))/(2*a);
printf("\n The roots of the equation are %.0fand%.0f",p,q);
}
if(d<0)
{
printf("\n The roots are imaginary");
r=(-b)/(2*a);
s=(sqrt((4*a*c)-(b*b)))/(2*a);
printf("\n The roots of the equation arep=%.2f=%.2fi,q=%.2f-%.2fi",r,s,r,s);
}
getch();
}
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / jijo alexander
#include<stdio.h>
void main()
{
int a,b,c;
float d,r1,r2;
printf("\n\n\t\tSolving the quadratic equation");
printf("\n\t\t********************************");
printf("\n\n\tEnter The Coefficent");
printf("\n\t======================");
/*Out put screen */
printf("\n\tCoefficent Of 'X^2' : ");
scanf(%d",&a);
printf("\n\tCoefficent Of 'X' : ");
scanf("%d",&b);
printf("\n\tConstant Value : ");
scanf("%d",&c);
d=(b*b)-(4*a*c);
if(d<0)
printf('\n\n\tImaginary Roots ");
else if(d==0)
{
r1=(-b)*(2*a);
printf("\n\tEqual Roots");
}
else
{
printf("\n\tReal roots : ");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\t %g , %g",r1,r2);
}
getch();
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / sachin kumar
#include<stdio.h>
int main()
{
int a,b,c,r1,r2,disc;
printf("Enter the number of a,b,c:");
scanf("%d\n%d\n%d",&a,&b,&c);
disc= b*b-4*a*c;
if(disc<0)
{
printf("Roots are imaginary");
}
else if(disc==0)
{
r1=(-b)/a;
r2=r1;
printf("The Root1 and Root2 are equal and roots are
%d\n,%d",r1,r2);
}
else
{
r1=(-b+((b*b)-4*a*c))/2*a;
r2=(-b-((b*b)-4*a*c))/2*a;
printf("The Root1 and Root2 are %d\n,%d\n",r1,r2);
}
}
Is This Answer Correct ? | 0 Yes | 0 No |
main() { extern out; printf("%d", out); } int out=100;
#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }
programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h
Is it possible to type a name in command line without ant quotes?
main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }
What is full form of PEPSI
Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
void main() { char ch; for(ch=0;ch<=127;ch++) printf(ā%c %d \nā, ch, ch); }
#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }
void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(ā%dā,k); }
write a c program to print magic square of order n when n>3 and n is odd?
Is it possible to print a name without using commas, double quotes,semi-colons?