×

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

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

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...

3 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.