Input: from the user Output: standard output AND file named \"program3.txt\" Wri
ID: 3873439 • Letter: I
Question
Input: from the user Output: standard output AND file named "program3.txt" Write a program that will calculate both roots of a quadratic equation. The program should ask the user to input a, b, and c and then write both roots to standard output (the screen) and a file named "program3.txt". Your program should print the roots ordered from least to greatest. If the quadratic equation has a negative discriminant your program should output "NO REAL ROOTS" If the two roots are the same only report one root ax2 + bx + c = 0 2a Sample Input 1, -3,-10 2, 5 3, 6, 3 2. -2,-12 2, 3 Sample Output -1 -,20ROOSExplanation / Answer
prog.c
#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
printf(" a : "), scanf("%d", &a);
printf(" b : "), scanf("%d", &b);
printf(" c : "), scanf("%d", &c);
int d = (b * b) - (4 * a * c);
FILE *fp = NULL;
fp = fopen("program3.txt", "w+");
if(fp == NULL)
{
fprintf(stderr, "%s", " Unable to open file ");
return (-1);
}
if(d < 0)
{
fprintf(stdout, "%s", " NO REAL ROOTS ");
fprintf(fp, "%s", "NO REAL ROOTS");
fclose(fp), fp = NULL;
return 0;
}
int e = sqrt(d);
int x1 = (-b) + e, x2 = (-b) - e;
x1 /= (2 * a), x2 /= (2 * a);
if(x1 == x2)
{
fprintf(stdout, "%d", x1);
fprintf(fp, "%d", x1);
}
else
{
int t;
if(x1 > x2)
{
t = x1;
x1 = x2;
x2 = t;
}
fprintf(stdout, "%d, %d", x1, x2);
fprintf(fp, "%d, %d", x1, x2);
}
fclose(fp), fp = NULL;
return 0;
}