Topic: Algorithms
Q1: Why computers are used?
A1: Computers are used to solve the problem.
Q2: What is Algorithm?
A2: It is effective step by step procedure with finite number of well
defined steps to solve a given problem.
Q3: What are the characteristics of Algorithm?
A3:
a. Input: An algorithm should have zero or more inputs.
b. Definiteness: Each instruction must be very clear. There shouldn't be
any ambiguity. Every instruction must clearly specify what must be
done.
c. Effectiveness: Each instruction must be carried out in finite amount
of time.
d. Finiteness: Algorithm should have finite number of instructions. It
should not enter in infinite loop.
e. Output: An algorithm should have at-least one output.
Q4: What are the notations used to write an Algorithm?
A4:
a. Name of Algorithm: Specifies the problem to be solved.
b. Start: Specifies where the algorithm begins.
c. Step Number: Identification tag of the instruction.
d. Explanatory comments: These are written in square brackets.
e. Termination (stop).
Q5: What word is used for input and output in an algorithm?
A5: For input: Read and for Output: Display
Q6: Write an algorithm to find sum and average of two numbers
A6:
Name of Algorithm: To find sum and average of two numbers
Step 1: Start
Step 2: Read two numbers as x and y
[input values for x and y]
Step 3: sum <- x + y
[Computing sum of x and y and assigning to variable sum]
Step 4: Display sum
[output sum]
Step 5: avg <- sum / 2;
[Compute average]
Step 6: Display avg [output average]
Step 7: Stop
Q7:Write an algorithm to calculate simple interest.
A7:
Name of Algorithm: To calculate simple interest
Step 1: Start
Step 2: Read principal_amount as P, rate_of_interest as R and time as T
Step 3: SI = (P*T*R)/100
[Computing simple interest]
Step 4: Display SI [Output simple interest]
Step 5: Stop
Q8: Write an algorithm to swap two numbers.
A8:
Name of Algorithm: To swap (Exchange) two numbers
Step 1: Start
Step 2: Read two numbers as x and y
[Input values for x and y]
Step 3: temp <- x
x<- y
y<- temp
Step 4: Display x and y [Output values after swapping]
Step 5: Stop
Q9: Identify errors in the following Algorithm
Name of Algorithm: To find sum of 2 numbers
Step 1: Start
Step 2: Display sum
Step 3: sum <- a + b
Step 4: Read a and b
Step 5: Stop
A9: We cannot use variables until we read them. So step 3 and 4 must be
swapped. Stop should be at last. We cannot display sum, till we compute
it. So we compute sum first then we display it.
Correct sequence:
Name of Algorithm: To find sum of 2 numbers
Step 1: Start
Step 2: Read a and b
Step 3: sum <- a + b
Step 4: Display sum
Step 5 : Stop
Q10: What are different types of algorithm?
A10:
a. Sequential
b. Conditional
c. Iterative / Repetitive
Q11: Write an Algorithm to find if a number is positive or negative
A11:
Name of Algorithm: To find if a number is positive or negative
Step 1: Start
Step 2: Read a Number A [Input]
Step 3: If A is greater than 0 [Compare with Zero]
Display “Number is positive”
Else
Display “Number is negative”
End If
Step 4: Stop [End of Algorithm]
Q12: Write an Algorithm to find largest of 2 Numbers.
A12:
Name of Algorithm: To find largest of 2 numbers
Step 1: Start
Step 2: Read 2 numbers as x and y [input]
Step 3: If x is greater than y [Compare x with y]
Display “X is greater than Y”
Else
Display “ Y is greater than X”
End if
Step 4: Stop
Q13: Write an Algorithm to find if number is even or odd
A13:
Name of Algorithm: To find if number is even or odd
Step 1: Start
Step 2: Read a Number N [input]
Step 3: If N is divisible by 2 then [Check if number is divisible by 2]
Display “N is Even”
Else
Display “N is Odd”
End if
Step 4: Stop
Q14: Write an algorithm to check if person is eligible for voting.
A14:
Name of Algorithm: To check if person is eligible for voting
Step 1: Start
Step 2: Read person's age as AGE [input]
Step 3: If AGE>=18 [Check age with 18]
Display “Eligible for voting”
Else
Display “Not Eligible for voting”
End if
Step 4: Stop
Q15: What are advantages and disadvantages of Algorithm
A15:
Advantages:
1. Effective communication.
2. Easy to identify mistakes
3. Easy to update
Disadvantages:
1. Developing algorithm for large and complex problems is time
consuming.
2. Difficult to understand logic for complex problems through algorithm