//Program 4.
A: Write a C program to evaluate quadratic equation of the form
ax2+bx+c=0.
Given a,b and c values.
#include <stdio.h>
#include <math.h> // For sqrt() function
int main()
{
double a, b, c, discriminant, root1, root2;
// Input coefficients a, b, and c
printf("Enter coefficients a, b, and c for the equation ax^2 + bx + c = 0:\n");
printf("a: ");
scanf("%lf", &a);
printf("b: ");
scanf("%lf", &b);
printf("c: ");
scanf("%lf", &c);
// Calculate the discriminant
discriminant = b * b - 4 * a * c;
// Check if the discriminant is positive, zero, or negative
if (discriminant > 0)
{
// Two real roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("The roots are real and different.\n");
printf("Root 1 = %.2lf\n", root1);
printf("Root 2 = %.2lf\n", root2);
}
else if (discriminant == 0)
{
// One real root (repeated root)
root1 = -b / (2 * a);
printf("The roots are real and the same.\n");
printf("Root = %.2lf\n", root1);
}
else
{
// Two complex roots
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("The roots are complex and different.\n");
printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);
printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);
}
return 0;
}
//Program 4.B: Develop a C program to convert decimal to binary number.
#include <stdio.h>
int main()
{
int decimal_number, binary[32], index = 0;
printf("Enter a positive decimal number: "); // Input decimal number from
user
scanf("%d", &decimal_number);
if (decimal_number == 0) // Handle zero input
{
printf("Binary: 0\n");
return 0;
}
while (decimal_number > 0) // Convert decimal to binary
by dividing by 2 and storing remainders
{
binary[index] = decimal_number % 2;
decimal_number = decimal_number / 2;
index++;
}
printf("Binary equivalent is : "); // Print binary in reverse
order
for (int i = index - 1; i >= 0; i--)
{
printf("%d", binary[i]);
}
printf("\n");
return 0;
}