Search This Blog

Monday 29 February 2016

Roots of Quadratic equation

Practical set 1A



#include <stdio.h>
#include <math.h>
void findandprint(float a,float b,float c) {
    float D,Ds,root1,root2,im1,im2;
    Ds = pow(b,2)-(4*c*a);
    if (Ds < 0) {
        D = sqrt(Ds * (-1));
        root1 = (-b)/(2*a);
        im1 =  D/(2*a);
        im2 = D/(2*a);
        printf("Roots are imaginary\n");
        printf("First Root : %f + %f i   \n Second Root : %f - %f i", root1,im1,root1,im2);
    }
    else {
        D = sqrt(Ds);
        root1 = ((-b)-D)/(2*a);
        root2 = ((-b)+D)/(2*a);
        if(root1==root2) {
            printf("Roots are real and same\n");
        }
        else {
            printf("Roots are real and distinct\n");
        }
        printf("First root : %f \n Second Root : %f",root1,root2);
    }
}

int main() {
    float a,b,c;
  
    printf("Enter a,b,c : ");
    scanf("%f %f %f",&a,&b,&c);
  
    findandprint(a,b,c);
    return 0;
}
      
      
  

No comments:

Post a Comment