In this tutorial you will be familiarize with the algorithm in C programming language.
Algorithm :
An algorithm is defined as a process or set of rules to be followed in calculations or other problem-solving operations or It can be defined as procedure or formula of solving a problem step by step i.e. sequentially or In algorithms the basic process/plan required to solve given problem is written.
Contents of algorithms :
- number of variables
- arithmetic or logical operation
- conditions
Qualities of good algorithms :
- Algorithms should be precise and easy to understand.
- Declaration of variables should be done in precise way.
- Each step should be clear and easy to understand.
- Algorithms should contain most effective and easy way to solve any problem.
- Algorithms should not contain similar programming code as algorithm is a basic plan or idea to develop a code.
Example of algorithms are given below:
- To swap two number
- To determine factorial of number entered by user
- to determine fibonacci series of number entered by user
Algorithm to swap two number is given below :
step 1. start
step 2. read two numbers from user( i.e. x and y)
step 3. declare a temporary variable
step 4. // for swapping
temp = x;
x=y;
y=temp;
step 5. exit
Algorithm to determine factorial of number entered by user is given below :
step 1. start
step 2. read number from the user(n)
step 3. initialize counter variable(i) with 0 to n
step 4. if ( i<=num ) go to step 5 otherwise return to step 7
step 5. calculate fact = fact * i
step 6. increment counter variable(i) and goto step 4
step 7. write fact
step 8. exit
Algorithm to determine fibonacci series of number entered by user is given below :
step 1. start
step 2. declare variables i, a,b, fib
step 3. initialize the variables, a=0 , b=1, and fib =0
step 4. Enter the number of terms of Fibonacci series to be printed
step 5. Print First two terms of series i.e. a and b
step 6. Use for-loop statement for the following steps
-> fib=a+b
-> a=b
-> b=fib
-> increase value of i each time by 1
-> print the value of show
step 7. exit
If you have any query please comment below.