Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //C Program to find the roots of a quadratic equation
- #include<stdio.h>
- #include<math.h>
- void main()
- {
- float a,b,c,d,root1,root2,real,imaginary;
- goto input;
- input:
- {
- printf("Enter the values of a, b & c of the quadratic equation ");
- scanf("%f%f%f",&a,&b,&c);
- }
- if(a==0)
- {
- printf("\nThe value of a cannot be zero. It is not a qadratic equation. Try again\n\n");
- goto input;
- }
- d=(b*b)-(4*a*c);
- if(d<0)
- {
- printf("\nThe roots are imaginary ");
- real=(-b)/(2*a);
- imaginary=sqrt(fabs(d))/(2*a);
- printf("and the roots are as follows\n R1 = %f + %fi\n R2 = %f + %fi",real,imaginary,real,imaginary);
- }
- else if(d==0)
- {
- printf("\nThe roots are real and equal ");
- root1=root2=(-b)/(2*a);
- printf("and the roots are as follows\n R1 = %f\n R2 = %f",root1,root2);
- }
- else
- {
- printf("\nThe roots are real and distinct ");
- root1=((-b)+(sqrt(d)))/(2*a);
- root2=((-b)-(sqrt(d)))/(2*a);
- printf("and the roots are as follows\n R1 = %f\n R2 = %f",root1,root2);
- }
- }
Add Comment
Please, Sign In to add comment