×

What is algorithm in C?

What is an algorithm?

  • In computer programming languages, an algorithm is set of statement that solves a particular problem.
  • In C, first step of every algorithm is Start and last step of every algorithm is End. The solution for a problem is represented within Start and End statements.
  • Consider a real world problem as a housewife wants to cook a dish, so she will first collect the ingredients, process the ingredients and finally make dish. The same steps are used to write an algorithm.

In simple words, algorithm can be summarized as:

  1. Take the input
  2. Perform some operations on the input to solve the problem
  3. Get the output

Characteristics of good algorithm

The characteristics of a good algorithm are as follows:

  1. Clear and Unambiguous: Each instruction in an algorithm should be unambiguous, i.e., there should be only one meaning of the statement and the meaning of it should be clear
  2. Input: The number of input can be zero or more than zero which will be given to the algorithm.
  3. Output: An algorithm should produce at least one result/output to the given problem.
  4. Finiteness: There should be finite number of steps in an algorithm and the algorithm shouldn’t go into infinite loop.
  5. Effectiveness: Each instruction should be effective as each instruction is affecting the entire algorithm.
  6. Language independent: An algorithm should be independent of language. Because if algorithm is language-independent then algorithm can solve the problem using any language.

Any algorithm that satisfies above all given characteristics is consider as a good algorithm.

How to write an Algorithm?

  1. Identify the problem which needs to be solved using algorithm.
  2. Identify the conditions of the problem that need to take into consideration while designing an algorithm.
  3. What input the algorithm will take.
  4. Identify what will be the output after successfully following each step of an algorithm
  5. At last, solution to the problem through the algorithm.

Examples

Example 1 – Algorithm to multiply two numbers in C

The algorithm to multiply the two numbers in C is as follows:

Step 1: Start

Step 2: Declare no1, no2 and mul_of_no as variables

Step 3: Read data for 2 variables or initialize these 2 variables.

Step 4: Multiply two variables and store the result in mul_of_no variable.

Step 5: Print the multiplication 

Step 6: End

Example 2 – Calculate the area and perimeter of a rectangle

The algorithm to calculate the area and perimeter of a rectangle are as follows:

Step 1: Start

Step 2: Declare variables length, breadth, area, and perimeter

Step 3: Read the values for variables length, breadth from user

Step 4: Calculate the area of rectangle as length*breadth and perimeter as 2*(length + breadth)

Step 5: Print the result that is variables area and perimeter

Step 6: End

Example 3 – Calculate factorial of a number

The algorithm to calculate factorial of a number is as follows:

Step 1: Start

Step 2: Declare the variables no, factorial.

Step 3: Read the variable no from the user and initialize factorial variable to 1

Step 4: Multiple the factorial variable with n and store the result in factorial variable and decrement the variable n by 1.

Step 5: Repeat the step 4 until no becomes 1.

Step 6: Print the result that is print the factorial variable.

Step 7: Stop


Related Topics

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.