2. a) Write a Program to Find the Roots of a Quadratic Equation using if else and Switch statements
Algorithm
Roots of quadratic equation
1. Start
2. Read a,b,c
3. If a==0, then write “the equation is not a quadratic equation.”,go to step 20
4. Set D=(b*b)-(4*a*c)
5. If (D>0) then set value:=1 then go to step 8
6. If (D<0) then set value:=2 then go to step 12
7. If (D=0) then set value:=3 go to step 16
8. Write “the roots are real and distinct.”
9. Set r1=(-b+sqrt(D))/(2*a)
10. Set r2=(-b-sqrt(D))/(2*a)
11. Write r1 and r2 ,go to step 19
12. Write “the roots are imaginary and distinct.”
13. Set r1=(-b+i*sqrt(fabs(D)))/(2*a)
14. Set r2=(-b-i*sqrt(fabs(D)))/(2*a)
15. Write r1 and r2 ,go to step 19
16. Write “the roots are real and equal.”
17. r1=r2=(-b)/(2*a)
18. Write r1 and r2
19. Stop
Flowchart:
Program
#include<stdio.h>
#include<math.h>
main(){
float a,b,c;
float d,root1,root2;
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(a<0)
{
printf("Not a Quadratic Equation\n");
return 0;
}
if(d < 0){
printf("Roots are complex number.\n");
printf("Roots of quadratic equation are: ");
printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
return 0;
}
else if(d==0)
{
printf("Both roots are equal.\n");
root1 = -b /(2* a);
printf("Root of quadratic equation is: %.3f ",root1);
return 0;
}
else
{
printf("Roots are real numbers.\n");
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);
}
}
Algorithm
Roots of quadratic equation
1. Start
2. Read a,b,c
3. If a==0, then write “the equation is not a quadratic equation.”,go to step 20
4. Set D=(b*b)-(4*a*c)
5. If (D>0) then set value:=1 then go to step 8
6. If (D<0) then set value:=2 then go to step 12
7. If (D=0) then set value:=3 go to step 16
8. Write “the roots are real and distinct.”
9. Set r1=(-b+sqrt(D))/(2*a)
10. Set r2=(-b-sqrt(D))/(2*a)
11. Write r1 and r2 ,go to step 19
12. Write “the roots are imaginary and distinct.”
13. Set r1=(-b+i*sqrt(fabs(D)))/(2*a)
14. Set r2=(-b-i*sqrt(fabs(D)))/(2*a)
15. Write r1 and r2 ,go to step 19
16. Write “the roots are real and equal.”
17. r1=r2=(-b)/(2*a)
18. Write r1 and r2
19. Stop
Flowchart:
Program
#include<stdio.h>
#include<math.h>
main(){
float a,b,c;
float d,root1,root2;
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(a<0)
{
printf("Not a Quadratic Equation\n");
return 0;
}
if(d < 0){
printf("Roots are complex number.\n");
printf("Roots of quadratic equation are: ");
printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
return 0;
}
else if(d==0)
{
printf("Both roots are equal.\n");
root1 = -b /(2* a);
printf("Root of quadratic equation is: %.3f ",root1);
return 0;
}
else
{
printf("Roots are real numbers.\n");
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);
}
}

Comments
Post a Comment