Home » Algorithm in C

Algorithm in C

Algorithm is a method that can be used by a computer for the solution of a problem. Algorithm is a sequence of instructions written in simple English language for solving a particular problem. The algorithm is independent of any programming language.

Properties of the Algorithm

  • Finiteness: An algorithm must always terminate after a finite number of steps.
  • Definiteness: Each step of an algorithm must be precisely defined: the actions to be carried out must be rigorously and unambiguously specified for each case.
  • Input: An algorithm has zero or more inputs; that is, quantities which are given to it initially before the algorithm begins.
  • Output: An algorithm has one or more outputs; that is, quantities which have a specified relation to the inputs.
  • Effectiveness: An algorithm is also generally expected to be effective. This means that all of the operations to be performed in the algorithm must be sufficiently basic that can in principle be done exactly and in a finite length of time.

Simply, An algorithm is a procedure or step-by-step instruction for solving a problem. They form the foundation of writing a program.

For writing any programs, the following has to be known:

  • Input
  • Tasks to be performed.
  • Output expected.

Complexity of an Algorithm: It is very convenient to classify algorithms based on the relative amount of time or relative amount of space they require and specify the growth of time/space requirements as a function of the input size. Thus, we have the notions of:

  • Time Complexity: Running time of the program as a function of the size of input.
  • Space Complexity: Amount of computer memory required during the program execution, as a function of the input size.

Example of an Algorithm: The following example of algorithm to check whether a number is positive or negative.

Step 1) : Read n, where n is any number.

Step 2) : If n>0, then write n is positive number.

else, n is negative number.

Step 3) : Stop.

Example of an Algorithm: The following example of algorithm to find the factorial of a given number.

Step 1) : Read n.

Step 2) : Set Fact = 1, I = 1.

Step 3) : Fact = Fact * I.

Step 4) : I = I + 1.

Step 5) : If I > n, write Fact

Else go to Step 3).

Step 6) : Stop.


Leave a comment

Your email address will not be published. Required fields are marked *

‘C’ Practical

  1. To get fomiliar with the structure of a ‘C’ program and print “Hello World!” on computer screen.
  2. To perform addition, subtraction, multiplication and division operations on two integers.
  3. To use unformatted 1/0 functions (getchar() and putchar(), gets() and puts()).
  4. To use scanf() function to read integers (%d, %i, %o, %u, %x), characters (%c), floating point numbers (%f, %g, %e), strings (%s, scanset).
  5. To use printf() function to format and print output (%d, %i, %o, %u, %x, %e, %E, %f, %g, %c, %p, %c, %s, %n)
  6. To change width, precision and alignment of the output of printf().
  7. To perform and, or, not, exor, left-shift and right-shift operations on integers.
  8. To check whether a given integer is even or odd (if … else statement).
  9. To write month name corresponding to a month number (switch … case).
  10. To check whether a given integer is prime using while loop.
  11. To reverse the digits of a given positive integer using while loop.
  12. To generate the multiplication table of a given integer using for loop.
  13. To generate first n terms of Fibonacci series using do-while loop.
  14. To compute factorial of a given integer using for loop.
  15. To compute sum of elements of a one dimensional integer array.
  16. To find the largest element in a one dimensional array.
  17. To perform matrix addition and matrix multiplication using 2-D arrays.
  18. To convert the alphabetic characters of a string to uppercase.
  19. To find the length of a string.
  20. To use Standard Library String Functions (strcat(), strrev(), strcp(), strcmp())
  21. Writing a function to find sum of two integers.
  22. Writing a function to swap value of two integers (call by reference).
  23. Writing a function to compute factorial of a given integer (using recursion).
  24. To read and print multiple data items (roll_no, name, branch, semester etc.) pertaining to an individual student.
  25. Demonstrating the use of union
  26. Demonstrating the use of address and dereferencing operators
  27. Performing pointer arithmetic to manipulate an array
  28. To read the contents from a file and display it on screen
  29. To read names and addresss of persons and write them into a file.
  30. Write a program to read the contents of two different files and write it into the third file.