Algorithm In C - Definition , properties of algorithm and some examples are explained
ALGORITHM :
It can be defined as step by step procedure for solving a problem . In order to solve a problem , first we write a algorithm . It contain step by step procedure . Next based upon a algorithm, we develop a program.
Properties Of Algorithm :
1) Input
2) Output
3) Finiteness
4) Definiteness
5) Effectiveness
INPUT :
An algorithm must accept either 0 or more number of inputs . So that means without supplying input also we can write the algorithm. Suppose our aim is printing a message like MOTHER . In this problem or program , there are no inputs . What we have to do is that we want to display a message called MOTHER . In this situation there's no need of input in algorithm.
OUTPUT :
An Algorithm must proceeded with 1 or more number of output. So that an algorithm must produce atleast 1 output . So without having output it is not possible to develop the algorithm. It has to proceed minimum of 1 output .
FINITENESS ( Means Countable ) :
An algorithm should contains finite number of steps .
Example : 2 steps , 10 steps , 100 steps or any number of steps but the step should be countable . That means algorithm should be terminated.
It should completes it's execution of that those finite number of steps .
DEFINITENESS :
Each step of an algorithm must be clear and unambiguous .
Clear means each instructions should be clear .
Eg : x=a+b; ✅ it is a valid instruction.
x=a$b;❌ it is a invalid instruction.
Each instruction should perform a proper operation. Dollar($) doesn't perform any operation.
So it implies that the instruction is not clear.
Unambiguous :
Each instructions should be unambiguous. That means corresponding instructions should perform only one operation .
Example :
x=a+b;
• We know what is addition (+) , so symbol should perform only one operation .
• + symbol should not perform multiplication, division , subtraction that is nothing but unambiguous.
The instructions should not be ambiguated.
EFFECTIVENESS :
All the operation can be performed in a finite amount of time using pen and pencil.
Let us take an example of our problem is Addition of two matrices
• we have to perform addition of two matrices using pen and pencil . So that means we have to solve any problem in a finite amount of time using pen and pencil.
Each algorithm should possess 5 properties of algorithm .
Let's see some examples of Algorithm :
Note :
I'm justing explain how to write algorithm for programming language in C . In examples also I've just written the algorithm part only . I've not explained the programs . Programs and all we will see on upcoming daily updates .
Example 1 :
Write an algorithm to display "Welcome C Programming".
Step 1 : Start . (starting step)
Step 2 : print"Welcome C Programming".
Step 3 : Stop .
Note :
● print is a keyword which is used to display the output.
● read is a keyword which is used to read the input .
Example 2 :
Write an algorithm to add two numbers .
Step 1 : Start .
Step 2 : read two numbers .
Step 3 : Calculate sum=a+b .
Step 4 : print sum
Step 5 : Stop .
Note :
● Input Operation (I/O) :
Read data from the keyboard .
● Output Operation (O/P) :
Displaying data on the monitor.
Example 3 :
Write an algorithm to calculate area & perimeter of a circle .
FORMULA :
Area of a circle = πr^2[ pie(π ) r square]
Perimeter of a circle = 2πr
Note:
π is constant value , so radius (r) will be the input
Step 1 : Start .
Step 2 : read r .
Step 3 : Calculate area = 3.14*r*r .
Step 4 :
Calculate perimeter =2*3.14*r .
Step 5 : print area .
Step 6 : print perimeter .
Step 7 : Stop .
Example 4 :
Write an algorithm to calculate sum of square of first 'n' natural number .
FORMULA :
Sum = (n*(n 1)*(2n 1))/6
Step 1 : Start .
Step 2 : read n .
Step 3 :
Calculate sum = (n*(n 1)*(2*n 1))/6 .
Step 4 : Stop .
Hope you all understand the concept ✌️ .
Drop your thoughts about this article in the comment section 👇.
Please Read my Study with Computer Knowledge Team
Enjoy Learning Programming Language !
Stay tuned for more concepts 🤞🏻 .