Tuesday, 17 July 2018

11-C Prog-6. Write a program to read the value of a variable of a quadratic equation and find the discriminant and give the root of the quadratic equation.

Algorithm
Input:
1).a    2.) b   3.) c
Ouput:
1.Roots are distinct or Roots are equal or Imaginary Root
Method:
1.Start
2. Read a, b and c
3. Write a, b and c
4. d= b*b-4*a*c
5. Write d
6. #logic
  if(d==0)
  begin
     r1=r2= -b /(2*a)
     Write "Root1=Root2= ",r1
  end
  else
  if( d>0)
  begin
     r1 = (-b + sqrt(d)  ) /( 2*a)
     r2 = (-b - sqrt(d)  ) /( 2*a)
     write" Root1 = ", r1
     write" Root2 = ", r2
  end
  else
  begin
      write "Roots are imaginary"
  end
7. stop
Example1:

Enter the a co-efficient  1
Enter the b co-efficient  2
Enter the c co-efficient  1
a= 1.0 b= 2.0 c= 1.0
d= 0.0
Root1=Root2= -1.0
 
Example2: 
Enter the a co-efficient 2
Enter the b co-efficient 5
Enter the c co-efficient 2
a= 2.0 b= 5.0 c= 2.0
d= 9.0
Root1= -0.5
Root2 -2.0 
Example3:
Enter the a co-efficient  3
Enter the b co-efficient  1
Enter the c co-efficient  2
a= 3.0 b= 1.0 c= 2.0
d= -23.0
Imaginary number



#Program

import math

a=float(input("Enter the a co-efficient   "))
b=float(input("Enter the b co-efficient  "))
c=float(input("Enter the c co-efficient  "))

print("a=",a,"b=",b,"c=",c)
d=b*b-4*a*c
print("d=",d)
#logic
if d==0:
    r1=r2=-b/(2*a)
    print("Root1=Root2=",r1)
else:
    if d>0:
        r1=(-b+math.sqrt(d))/(2*a)
        r2=(-b-math.sqrt(d))/(2*a)
        print("Root1=",r1)
        print("Root2",r2)
    else:
        print("Imaginary number")      


No comments:

Post a Comment