Ex1. Write an Algorithm and flowchart for swapping contents of two variables.
Solution:
Algorithm: Swapping contents of two variables
Step1: Start
Step 2: Declare variables N1, N2, TEMP
Step 3: Read N1, N2, TEMP
Step 4: TEMP=N1
N1=N2
N2=TEMP
Step 5: Print N1, N2
Step 6: Stop
Flowchart: Swapping contents of two variables
Ex2. Write an Algorithm and flowchart to find largest of three numbers.
Solution:
Algorithm: To find the largest of three numbers
Step1: Start
Step 2: Declare three variables A, B, C;
Step 3: Read three variables A, B, C;
Step 4: Check if A is greater than B
If true, then check if A is greater than C.
If true, print 'A' as the greatest number.
If false, print 'C' as the greatest number.
If false, then check if B is greater than C.
If true, print 'B' as the greatest number.
If false, print 'C' as the greatest number.
Step 5: End
Flowchart: To find the largest of three numbers
Ex3. Write an Algorithm and flowchart to find the factorial of a number.
Solution:
Algorithm to calculate the factorial value of a number:
Step 1: Start
Step 2: Read the number n
Step 3: [Initialize]
i=1, fact=1
Step 4: Repeat Step 4 through 6 until i=n
Step 5: fact=fact*i
Step 6: i=i+1
Step 7: Print fact
Step 8: Stop
Flowchart to calculate the factorial value of a number:
Ex4. Write an Algorithm and flowchart for solving a given quadratic equation
ࢇ࢞ + ࢈࢞ + ࢉ =
Solution:
Algorithm for solving a given quadratic equation
Step 1: Start
Step 2: Read a, b, c
Step 3: d(b x b - 4 x a x c)
Step 4: x1(-b+d)/2 x a
Step 5: x2(-b-d)/2 x a
Step 6: Print x1, x2
Step 7: Stop
Flowchart for solving a given quadratic equation
Divide and Conquer algorithm
Divide and Conquer is an algorithmic paradigm. A typical Divide and Conquer algorithm solves
a problem using following three steps.
1. Divide:: Break the given problem into subproblems of same type.
2. Conquer:: Recursively solve these subproblems
3. Combine:: Appropriately combine the answers
A classic example of Divide and Conquer is Merge Sort demonstrated below. In Merge Sort, we
divide array into two halves, sort the two halves recursively, and then merge the sorted halves.
halves