Algorithm In C Language Programming
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.
![]() |
Fig: Algorithm |
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.
What are the Characteristics of an Algorithm?
![]() |
Fig: Characteristics Of Algorithm |
All written instructions for programming is an
algorithm. In order for some instructions to be an algorithm, it must have the
following characteristics:
· Clear and Unambiguous: Algorithm should be clear and
unambiguous. Each of its steps should be clear in all aspects and must lead to
only one meaning.
· Well-Defined Inputs: If an algorithm says to take inputs, it
should be well-defined inputs.
· 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.
· Language Independent: The Algorithm designed must be
language-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.
To Design an Algorithm
Inorder to write an algorithm, following things are
needed as a pre-requisite:
1. The problem that is to be solved by this algorithm.
2. The constraints of the problem that must be considered
while solving the problem.
3. The input to be taken to solve the problem.
4. The output to be expected when the problem the is
solved.
5. The solution to this problem, in the given constraints.
Then
the algorithm is written with the help of above parameters such that it solves
the problem.
Example: The example to add three numbers and print the sum.
Step 1: Fulfilling the pre-requisites
As discussed above, in order to
write an algorithm, its pre-requisites must be fulfilled.
1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
2. The constraints of the problem that must be considered while
solving the problem: The numbers must contain only
digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem the is solved: The sum of the three numbers taken as the input.
5. The solution to this problem, in the given constraints: The solution consists of adding the 3 numbers. It can
be done with the help of ‘+’ operator, or bit-wise, or any other method.
Step
2: Designing the algorithm
The algorithm with
the help of above pre-requisites:
Algorithm
to add 3 numbers and print their sum:
START
Declare 3 integer variables num1, num2 and num3.
Take the three numbers, to be added, as inputs in variables
num1, num2, and num3 respectively.
Declare an integer variable sum to store the resultant sum
of the 3 numbers.
Add the 3 numbers and store the
result in the variable sum.
Print the value of variable sum
END
Step
3: Testing the algorithm by implementing it.
Inorder to test the algorithm, let’s
implement it in C language.
// C program to add three numbers
// with the help of
above designed algorithm
#include
<stdio.h>
int main()
{
// Variables to take the input of the 3 numbers
int num1, num2, num3;
// Variable to store the resultant sum
int sum;
// Take the 3 numbers as input
printf("Enter the 1st number: ");
scanf("%d", &num1);
printf("%d\n", num1);
printf("Enter the 2nd number: ");
scanf("%d", &num2);
printf("%d\n", num2);
printf("Enter the 3rd number: ");
scanf("%d", &num3);
printf("%d\n", num3);
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;
// Print the sum
printf("\nSum of the 3 numbers is: %d", sum);
return 0;
}
Output:
Enter 1st
number:4
Enter 2nd
number:5
Enter 3rd
number:1
Sum of the 3
numbers is:10
· Clear and Unambiguous: Algorithm should be clear and
unambiguous. Each of its steps should be clear in all aspects and must lead to
only one meaning.
· Well-Defined Inputs: If an algorithm says to take inputs, it
should be well-defined inputs.
· 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.
· Language Independent: The Algorithm designed must be
language-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.
To Design an Algorithm
Inorder to write an algorithm, following things are
needed as a pre-requisite:
1. The problem that is to be solved by this algorithm.
2. The constraints of the problem that must be considered
while solving the problem.
3. The input to be taken to solve the problem.
4. The output to be expected when the problem the is
solved.
5. The solution to this problem, in the given constraints.
Then
the algorithm is written with the help of above parameters such that it solves
the problem.
Example: The example to add three numbers and print the sum.
Step 1: Fulfilling the pre-requisites
As discussed above, in order to
write an algorithm, its pre-requisites must be fulfilled.
1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
2. The constraints of the problem that must be considered while
solving the problem: The numbers must contain only
digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem the is solved: The sum of the three numbers taken as the input.
5. The solution to this problem, in the given constraints: The solution consists of adding the 3 numbers. It can
be done with the help of ‘+’ operator, or bit-wise, or any other method.
Step 2: Designing the algorithm
The algorithm with
the help of above pre-requisites:
Algorithm to add 3 numbers and print their sum:
START
Declare 3 integer variables num1, num2 and num3.
Take the three numbers, to be added, as inputs in variables
num1, num2, and num3 respectively.
Declare an integer variable sum to store the resultant sum
of the 3 numbers.
Add the 3 numbers and store the
result in the variable sum.
Print the value of variable sum
END
Step 3: Testing the algorithm by implementing it.
Inorder to test the algorithm, let’s
implement it in C language.
// C program to add three numbers
// with the help of
above designed algorithm
#include
<stdio.h>
int main()
{
// Variables to take the input of the 3 numbers
int num1, num2, num3;
// Variable to store the resultant sum
int sum;
// Take the 3 numbers as input
printf("Enter the 1st number: ");
scanf("%d", &num1);
printf("%d\n", num1);
printf("Enter the 2nd number: ");
scanf("%d", &num2);
printf("%d\n", num2);
printf("Enter the 3rd number: ");
scanf("%d", &num3);
printf("%d\n", num3);
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;
// Print the sum
printf("\nSum of the 3 numbers is: %d", sum);
return 0;
}
Output:
Enter 1st
number:4
Enter 2nd
number:5
Enter 3rd
number:1
Sum of the 3
numbers is:10
No comments:
Post a Comment