Coding Practice

Write a C Code to calculate D = 𝒃^𝟐 βˆ’ πŸ’πšπœ and...

Write a C Code to calculate D = 𝒃^𝟐 βˆ’ πŸ’πšπœ and show the result according to following conditions:

a. D>0 then Calculate X1, X2. Where π’™πŸ = (βˆ’π’ƒ+βˆšπ’ƒ^πŸβˆ’πŸ’π’‚π’„)/πŸπ’‚ and π’™πŸ = (βˆ’π’ƒβˆ’βˆšπ’ƒ^πŸβˆ’πŸ’π’‚π’„)/πŸπ’‚

        b. D =0 then calculate X. Where 𝐱 = 𝒃^𝟐/πŸπ’‚

        c. D<0 then print β€œNo Solution”.

Sample Input

        Enter a, b & c: 5 6 1

Sample Output

    X1: -0.2

    X2: -1
Sample Output
Enter a, b, & c: 5 6 1
X1: -0.200
x2: -1.000
Source Code
#include<stdio.h>
#include<math.h>
int main()
{
float D, b, a, c, x, x1, x2;
printf("Enter a, b, & c: ");
scanf("%f %f %f", &a, &b, &c);
D = ((b*b) - 4*a*c);
if(D > 0)
{
x1 = (-b + sqrt(D))/(2*a);
x2 = (-b - sqrt(D))/(2*a);
printf("X1: %.3f\nx2: %.3f\n\n", x1, x2);
}
else if(D == 0)
{
x = (b*b)/(2*a);
printf("X: %f\n\n");
}
else if(D < 0)
{
printf("No Solution\n\n");
}
return 0;
}
Sample Output
Enter a, b, & c: 5 6 1
X1: -0.200
x2: -1.000

No comments:

Post a Comment

Change Theme
X