Page 108 - Computer science 868 Class 12
P. 108

Step 3:  Calculate discriminant d = sqrt (b × b - 4 × a × c).
              Step 4:  If d < 0 then print roots are imaginary, go to Step 12.

              Step 5:  If d = 0 then print roots are real and equal, go to Step 6 else go to Step 8.
              Step 6:  Calculate r1 = -b/(2 x a).
              Step 7:  Print “Roots are”, r1 and go to Step 12.

              Step 8:  Calculate first root r1 = (-b + d)/(2 × a).
              Step 9:  Calculate second root r2 = (-b - d)/(2 × a).
              Step 10: Print “Roots are real and unequal”.

              Step 11: Print r1 and r2.
              Step 12: Stop.

              Problem 3: Write an algorithm to enter distance travelled by a cab and calculate fare as follows:

                        Distance travelled        Fare
                        up to 1 km                `30

                        Next 5 km                 `10 per km
                        Next 10 km                `8 per km
                        Beyond 16 km              `6 per km

              Step 1:  Start.
              Step 2:  Accept distance d.

              Step 3:  If d<=1 then fare f=30, go to Step 7.
              Step 4:  If d<=6 then f=30+(d-1)×10, go to Step 7.

              Step 5:  If d<=16 then f=30+5×10+(d-6)×8, go to Step 7.
              Step 6:  f=30+5×10+8×10+(d-16)×6.

              Step 7:  Print f.
              Step 8:  Stop.


              3.5.2 Algorithm of Decision and Iterative Statements
              Problem 4: Write algorithm to check if a number is Perfect number or not. A perfect number is one where Sum of
              the factors excluding itself is equal to the number.

              Step 1:  Start.
              Step 2:  Accept number in n

              Step 3:  Initialize i to 1 and Sum to 0
              Step 4:  Repeat Step 5 to Step 6 while i<n

              Step 5:  If n%i=0 add i to Sum

              Step 6:  Increase i by 1
              Step 7:  If Sum=n then print “Perfect” else “not”

              Step 8:  Stop.


                106106  Touchpad Computer Science-XII
   103   104   105   106   107   108   109   110   111   112   113