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