Lecture 10 (Algorithms)
Lecture 10 (Algorithms)
What is Algorithm?
The word Algorithm means “a process or set of rules to be followed in
calculations or other problem-solving operations”.
Therefore Algorithm refers to a set of rules/instructions that step-by-step define
how a work is to be executed upon in order to get the expected results.
It can be understood by taking an example of cooking a new recipe. To cook a new
recipe, one reads the instructions and steps and execute them one by one, in the given
sequence. The result thus obtained is the new dish cooked perfectly. Similarly,
algorithms help to do a task in programming to get the expected output.
The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language, and yet the output will be the
same, as expected.
Characteristics of an Algorithm?
Well-Defined Outputs: The algorithm must clearly define what output will
be yielded and it should be well-defined as well.
Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite
loops or similar.
Feasible: The algorithm must be simple, generic and practical; such that it can
be executed upon will the available resources. It must not contain some future
technology, or anything.
independent, i.e. it must be just plain instructions that can be implemented in any
language, and yet the output will be same, as expected.
consists of adding the 2 numbers. It can be done with the help of „+‟ operator, or
bit-wise, or any other method.
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 sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
// C program to add two numbers with the help of above designed algorithm
#include <stdio.h>
main()
{
int num1, num2; // Variables to take the input of the 2 numbers
int sum; // Variable to store the resultant sum
printf("Enter the 1st number: ");
scanf("%d", &num1); // Take the 2 numbers as input
printf("Enter the 2nd number: ");
scanf("%d", &num2);
sum = num1 + num2 // Calculate the sum using + operator and store it in variable sum.