top of page

What is an algorithm

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Dec 7, 2024
  • 3 min read

What is an Algorithm?

An algorithm is a well-defined, step-by-step procedure used to solve a specific problem or perform a task. It provides a systematic approach for problem-solving, ensuring the task is completed efficiently and effectively.


Understanding Algorithms


An algorithm is a clear, finite set of instructions designed to solve a specific problem or accomplish a task. In programming, algorithms determine how data flows and operations are performed. While you can express algorithms in various formats like natural language, pseudocode, or flowcharts, programming languages like C enable the effective implementation of these instructions.


Key Characteristics of an Algorithm

  1. Input: Accepts zero or more inputs.

  2. Output: Produces at least one result.

  3. Definiteness: Each step must be precisely defined and unambiguous.

  4. Finiteness: Must terminate after a finite number of steps.

  5. Effectiveness: All steps should be simple enough to be executed in a finite amount of time.


Purpose of an Algorithm

  • To serve as a blueprint for solving problems.

  • To provide a logical foundation for writing computer programs.

  • To optimize the use of resources like time and memory.


Representation of Algorithms

  1. Natural Language: Described using plain language.

  2. Pseudocode: Uses structured, human-readable syntax.

  3. Flowcharts: Uses symbols and diagrams for visualization.


Example Algorithm

Problem: Find the largest of three numbers.

Steps:

  1. Start.

  2. Input three numbers: A, B, C.

  3. If A > B and A > C, then A is the largest.

  4. Else if B > A and B > C, then B is the largest.

  5. Else, C is the largest.

  6. Output the largest number.

  7. Stop.


Here's a C program to input three numbers and find the largest among them:

#include <stdio.h> // Include the standard input/output header file

int main() {
   // Declare variables to store the three numbers
    int A, B, C;

    // Input three numbers
    printf("Enter three numbers: ");
    scanf("%d %d %d", &A, &B, &C);

    // Compare and find the largest number
    if (A >= B && A >= C) {

       printf("The largest number is: %d\n", A);
    } else if (B >= A && B >= C) {
        printf("The largest number is: %d\n", B);
    } else {
        printf("The largest number is: %d\n", C);
    }

    return 0; // Exit the program
}

Explanation of the Program

  1. Input Section:

    • The scanf() function reads three integer values entered by the user and stores them in variables A, B, and C.

  2. Logic Section:

    • The if statement compares A, B, and C to determine the largest.

    • Logical operators (&&): Ensure that all conditions are satisfied.

  3. Output Section:

    • The printf() function displays the largest number.


Sample Run


Input:

Enter three numbers: 5 15 10


Output:

The largest number is: 15

Let me know if you need additional modifications!


The representation of an algorithm refers to how it is documented or visualized. Algorithms can be represented in various ways depending on the context, audience, or purpose. Below are the most common methods:


1. Natural Language Representation

The steps of the algorithm are described in plain, human-readable language.


Example:

Find the largest of three numbers:


  1. Start.

  2. Input three numbers: A, B, C.

  3. If A > B and A > C, then A is the largest.

  4. Else if B > C, then B is the largest.

  5. Otherwise, C is the largest.

  6. Print the largest number.

  7. Stop.


2. Pseudocode

Pseudocode uses structured, informal programming-like syntax. It’s more precise than natural language but not as strict as real code.


Example:

Start
Input A, B, C
If A > B and A > C
    Largest ← A
Else if B > C
    Largest ← B
Else
    Largest ← C
Print Largest
Stop

3. Flowchart


A flowchart uses graphical symbols to depict the sequence of operations in an algorithm.


Key Symbols:

  • Oval: Start/End

  • Rectangle: Process (e.g., calculations, assignments)

  • Diamond: Decision (e.g., if/else conditions)

  • Arrow: Flow of control


Example: Flowchart for finding the largest of three numbers:


  1. Start

  2. Input A, B, C

  3. Compare A, B, and C using decision diamonds

  4. Output the largest number

  5. End


    This is flow chart of program;



4. Tabular Representation

Steps and conditions are written in a tabular format for clarity.

Step

Operation

Condition

Result

1

Input three numbers A, B, C



2

Compare A with B and C

A > B and A > C

A is largest

3

Compare B with C

B > C

B is largest

4

Default


C is largest

5

Print the largest number



Choosing a Representation

  • Natural Language: Best for explaining to non-programmers.

  • Pseudocode: Ideal for programmers and students learning coding concepts.

  • Flowcharts: Useful for visualizing complex logic.

  • Tabular Representation: Effective for summarizing conditions and actions.

 
 
 

Comments


bottom of page