# Introduction to Algorithms

## What is an algorithm?

The Paritosh Kumar
·May 13, 2022·

• What is an Algorithm?

# What is an Algorithm?

Algorithms are the sequence of instructions that takes input, process the data, and produces the desired output.

In other words, algorithms are sets of well-defined instructions to solve a problem.

### For example:

Algorithm to add two numbers:

• Take two numbers
• Add them using + operator
• Display output of two numbers

### Characteristics of Good Algorithms:

• Input and Output should be well-defined.
• The algorithm should be clear and unambiguous.
• Algorithms should be most effective and optimized.
• The algorithm should be written in such a way that it can be used in different programming languages.

## Examples of Algorithms

### 1. Sum of two Numbers

``````Step 1: Start
Step 2: Declare variables num1, num2, and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to the sum.
sum←num1+num2
Step 5: Display the sum
Step 6: Stop
``````

### 2. Find the largest number among three numbers

``````Step 1: Start
Step 2: Declare variables num1, num2, and num3.
Step 3: Read variables num1, num2, and num3.
Step 4: If num1 > num2
If num1 > num3
Display num1 is the largest number.
Else
Display num3 is the largest number.
Else
If num2 > num3
Display num2 is the largest number.
Else
Display num3 is the greatest number.
Step 5: Stop
``````

### 3. Find Root of the quadratic equation \$\$ ax^2 + bx + c = 0 \$\$

``````Step 1: Start
Step 2: Declare variables a, b, c, Discriminant, root and imaginary,root1, root2;
Step 3 : Read values of a,b,c;
Step 4: Calculate discriminant
Discriminant ← b*b-4*a*c
Step 5: If D ≥ 0
root1 ← (-b+√Discriminant)/2*a
root2 ← (-b-√Discriminant)/2*a
Display root1 and root2 as roots.
Else
Calculate real part and imaginary part
root ← -b/2*a
imaginary ← √(-Discriminant)/2*a
Display root+j(imaginary) and root-j(imaginary) as roots
Step 6: Stop
``````

### 4. Find the factorial of a number

``````Step 1: Start
Step 2: Declare variables num, factorial and i.
Step 3: Initialize variables
factorial ← 1
i ← 1
Step 4: Read value of num
Step 5: Repeat the steps until i = num
factorial ← factorial*i
i ← i+1
Step 6: Display factorial
Step 7: Stop
``````

### 5. Check whether a number is prime or not

``````Step 1: Start
Step 2: Declare variables num, i, flag.
Step 3: Initialize variables
flag ← 1
i ← 2
Step 4: Read num from the user.
Step 5: Repeat the steps until i=(num/2)
If remainder of num÷i equals 0
flag ← 0
Go to step 6
i ← i+1
Step 6: If flag = 0
Display num is not prime
else
Display num is prime
Step 7: Stop
``````

### 6. Find the Fibonacci series till 1000

``````Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
temp ← second_term
second_term ← second_term + first_term
first_term ← temp
Display second_term
Step 6: Stop
``````