Skip to main content

Compute the roots of a quadratic equation by accepting the coefficients Program and Algorithm Computer Science Engineering VTU

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
    float a,b,c,x1,x2,sp,ip,disc;
    printf("enter the co-efficients\n");
    scanf("%f%f%f",&a,&b,&c);
    if(a==0)
    {
        printf("a cannot be zero");
        exit(0);
    }
    disc=b*b-4*a*c;
    if(disc==0)
    x1=x2=-b/(2*a);
    printf("the roots are equal\n");
    printf("x1=%f\n x2=%f\n",x1,x2);
}
    else if(disc>0)
{
    x1=(-b+sqrt(disc))/(2*a);
    x2=(-b-sqrt(disc))/(2*a);
    printf("the roots are real and distinct\n");
    printf("x1=%f\n x2=%f\n";x1,x2);
}
else 
{
    sp=-b/(2*a);
    ip=sqrt(fabs(disc))/(2*a);
    printf("the roots are Complex\n");
    printf("first root=%f+i%f\n",sp,ip);
    printf("second root=%f-i%f\n",sp,ip);

Algorithm

Start 

Read the co-efficient as a, b, c

if a is equal to 0 display error 

else compute disc=(b*b)-(4*a*c) and r=sqrt(fabs(disc)))

if disc is equal to 0 then 

compute x1=x2= -b/(a*2.0)) and display x1,x2

else If discriminant is greater than 0 then 

compute (x1=(-b+r)/(a*2.0)), x2= (-b-r)/(a*2.0)) and display x1,x2

else If discriminant is lesser than zero, 

display roots are complex 

compute x1=(-b)/(a*2.0)+ir/(a*2.0) and x2= (-b)/(a*2.0)-ir/(a*2.0) and display x1,x2

Stop

Comments

Popular posts from this blog

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

STRINGconcatenation #include<stdio.h> #include<stdlib.h> int length (char str[]); int compare (char str1[],char str2[]); void concatenate (char str1[],char str2[]); void main() { char str1[30],str2[30]; int choice,a,i,j; printf("enter 1-string comparision\n"); printf("enter 2-string length\n"); printf("enter 3-string concatenation\n"); printf("enter 4-exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("enter string 1\n"); scanf("%s",str1); printf("enter string 2\n"); scanf("%s",str2); a=compare(str1,str2); if(a==0) { printf("%s and%s are identical\n",str1,str2); } else { printf("%s and%s are not identical\n",str1,str2); } break; ...

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are quarantined to prevent the spread of bird flu.

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are  quarantined  to prevent the spread of bird flu. immunized butchered secluded CORRECT mingled