Custom Search

Sunday, 15 January 2012

WAP, Write a C or C++ Program to compute real roots of quadratic equation with output

WAP, Write a C or C++ Program to compute real roots of quadratic equation with output


Program
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
{
float a,b,c,d,r1, r2;
clrscr();
printf("Enter the value of a,b,c:\n");
scanf("%f %f %f ", &a, &b, %c);
d= b*b -4*a*c;
printf ("discriminant is %f", d);
if(d<0)
{
printf("\n Roots are imaginary");
}
else
{
r1= (-b+ sqrt(d))/(2*a);
r2= (-b- sqrt(d))/2*a);
printf ("\n %f  %f ", r1, r2);
}
getch();
}


Output
Enter the values of a,b,c:
2
5
2
discrimant is: 9.0
-0.50 -2.0



Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.

No comments:

Post a Comment

Laptops