0% found this document useful (0 votes)
29 views3 pages

LAB Mannual - POP

Pop lab manual

Uploaded by

Durai M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

LAB Mannual - POP

Pop lab manual

Uploaded by

Durai M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Program-1

Compute the roots of a quadratic equation by accepting the coefficients.


Print appropriate messages.
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c;
double discriminant, root1, root2, realPart, imaginaryPart;
// Input coefficients
printf("Enter coefficient a: ");
scanf("%lf", &a);
printf("Enter coefficient b: ");
scanf("%lf", &b);
printf("Enter coefficient c: ");
scanf("%lf", &c);
// Check if the equation is quadratic
if (a == 0)
{
printf("The value of 'a' cannot be zero. This is not a quadratic
equation.\n");
return 0;
}
// Calculate the discriminant
discriminant = b * b - 4 * a * c;
// Check the nature of the roots
if (discriminant > 0)
{
// Two distinct real roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The equation has two distinct real roots:\n");
printf("Root 1: %.2f\n", root1);
printf("Root 2: %.2f\n", root2);
}
else if (discriminant == 0)
{
// One real root (repeated)
root1 = -b / (2 * a);
printf("The equation has one real root (repeated):\n");
printf("Root: %.2f\n", root1);
}
else
{
// Two complex roots
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("The equation has two complex roots:\n");
printf("Root 1: %.2f + %.2fi\n", realPart, imaginaryPart);
printf("Root 2: %.2f - %.2fi\n", realPart, imaginaryPart);
}
return 0;
}
OUTPUT:
1) Enter coefficient a: 1
Enter coefficient b: -7
Enter coefficient c: 10
The equation has two distinct real roots:
Root 1: 5.00
Root 2: 2.00
2)Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 5
The equation has two complex roots:
Root 1: -1.00 + 2.00i
Root 2: -1.00 - 2.00i
3)Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 1
The equation has one real root (repeated):
Root: -1.00

You might also like