Algorithm in programming can be defined as a sequence of activities to be processed for getting desired output from a given input.Or in other words it is a formula or set of steps for solving a particular problem. To be an algorithm, a set of rules must be unambiguous and have a clear stopping point.
Learn about programming chart or flow chart.
Algorithm in programming
From the above Data we can say that:
1. Getting specified output is essential after algorithm is executed.
2. One will get output only if algorithm stops after finite time.
3. Activities in an algorithm to be clearly defined in other words for it to be unambiguous.
Before writing an algorithm for a problem, one should find out what is/are the inputs to the algorithm and what is/are expected output after running the algorithm. Now let us take some exercises to develop an algorithm for some simple problems: While writing algorithms we will use following symbol for different operations:
'+' for Addition
'-' for Subtraction
'*' for Multiplication
'/' for Division and
' ' for assignment.
For example A X*3 means A will have a value of X*3. Lets look at some problems on algorithm.
Problem 1: Write an algorithm to Find the area of a Circle of radius r.
Inputs to the algorithm:
Radius r of the Circle.
Area of the Circle
Step1: Readinput the Radius r of the Circle
Step2: Area PI*r*r // calculation of area
Step3: Print Area
Problem 2: Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm:
Sum of the two numbers.
Step2: Readinput the first num1.
Step3: Readinput the second num2.
Step4: Sum num1+num2 // calculation of sum
Step5: Print Sum
|Write an algorithm easily|
Type of AlgorithmsThe algorithm and flowchart, classification to the three types of control
structures. They are:
- Branching (Selection)
- Loop (Repetition)
These three control structures are sufficient for all purposes.
The sequence is exemplified by sequence of statements place one after the other – the one above or before another gets executed first.
The branch refers to a binary decision based on some condition. If the condition is true, one of the two branches is explored; if the condition is false, the other alternative is taken. This is usually represented by the "if-then" construct in pseudo-codes and programs. Lets see an example.
Problem 3: write algorithm to find the greater number between two numbers
Step1: StartThe loop allows a statement or a sequence of statements to be repeatedly executed based on some loop condition. It is represented by the 'while' and 'for' constructs in most programming languages, for unbounded loops and bounded loops respectively. Lets see an example.
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Problem 4: An algorithm to calculate even numbers between 0 and 99
2. I ← 0
3. Write I in standard output
4. I ← I+2
5. If (I <=98) then go to line 3
Properties of algorithmHere is a list of five properties for an algorithm, these properties are:
1) Finiteness: An algorithm must always terminate after a finite number of steps. It means after every step one reach closer to solution of the problem and after a finite number of steps algorithm reaches to an end point.
2) Definiteness: Each step of an algorithm must be precisely defined. It is done by well thought actions to be performed at each step of the algorithm. Also the actions are defined unambiguously for each activity in the algorithm.
3) Input: Any operation you perform need some beginning value/quantities associated with different activities in the operation. So the value/quantities are given to the algorithm before it begins.
4) Output: One always expects output/result (expected value/quantities) in terms of output from an algorithm. The result may be obtained at different stages of the algorithm. If some result is from the intermediate stage of the operation then it is known as intermediate result and result obtained at the end of algorithm is known as end result. The output is expected value/quantities always have a specified relation to the inputs
5) Effectiveness: Algorithms to be developed/written using basic operations. Actually operations should be basic, so that even they can in principle be done exactly and in a finite amount of time by a person, by using paper and pencil only.
Learn about Programming language in Demand
Algorithm in programming can be learned by practice. Please share this post on Facebook and twitter and with every one else. Thank you for reading this post. Ask your questions in comment section.