Saturday 17 August 2019

Solution of quadratic equation C program


/*Solution of quadratic equation*/


#include<stdio.h>
#include<math.h>
void
main()
{
            float a,b,c,d,x1,x2,rp,imp;
            printf("enter the value of a,b,c");
            scanf("%f%f%f",&a,&b,&c);
            d=b*b-4.0*a*c;
            if (d>=0)
            {
                        printf("roots are real");
                        x1=(-b+sqrt(d))/(2.0*a);
                        x2=(-b-sqrt(d))/(2.0*a);
                        printf("\n x1= %f,x2= %f\n",x1,x2);
            }
            else
            {
                        printf("roots are complex");
            rp=-b*(2.0*a);
            imp=sqrt(abs(d))/(2.0*a);
            printf("first root = %f+ %fi\n",rp,imp);
            printf("second root= %f-%fi\n",rp,imp);
            }
}




/*Solution of quadratic equation */
#include<stdio.h>
#include<math.h>
void
main()
{
                float a,b,c,d,x1,x2,rp,imp;
                printf(" enter the value of a,b,c");
                scanf("%f%f%f",&a,&b,&c);
                d=(b*b)-(4.0*a*c);
                if(d>=0)
                {
                                printf("roots are real");
                                x1=(-b+sqrt(d))/(2.0*a);
                                x2=(b-sqrt(d))/(2.0*a);
                                printf("\n x1=%f,x2=%f\n",x1,x2);
                }
                else
                {
                                printf("roots are complex");
                                rp=-b/(2.0*a);
                                imp=sqrt(abs(d))/(2.0*a);
                                printf("1st root=%f+%fi\n",rp,imp);
                                printf("2nd root=%f-%fi\n",rp,imp);
                }
}
Result1:
enter the value of a,b,c2 3 1
roots are real
 x1=-0.500000,x2=0.500000
Press any key to continue
Result2:
enter the value of a,b,c2 4 2
roots are real
 x1=-1.000000,x2=1.000000
Press any key to continue
Result3:
enter the value of a,b,c2 3 5
roots are complex1st root=-0.750000+1.391941i
2nd root=-0.750000-1.391941i

Press any key to continue

No comments:

Post a Comment

Thanks for comment.