What is an algorithm? Explaination with an example

Algorithms can be defined as the set of instructions structured to solve a problem. It is a step-by-step procedure to solve the problem. These steps are generally written in English to understand them easily and later convert them into computer programs. These steps together are called algorithms.

Let us consider an example, suppose you went shopping and want to know the amount you spent. So, you take out the shopping list and start adding the prices to your calculator.The same work can be done by designing an algorithm that adds your total amount.

Algorithm

 1. Start
 2. Assign a variable to each item (item1 name,item2 name,….).
 3. Create a variable “total” which will store the total price.
 4. Add all the “items” and store it to “total”.
 5. Display “total”.
 6. Stop. 

Here is a C program to implement the above algorithm

#include <stdio.h>
int main()
{
    int milk, rice, potato, totalPrice;
    printf("enter the price of milk \n");
    scanf("%d",&milk);
    printf("enter the price of rice \n");
    scanf("%d",&rice);
    printf("enter the price of potato \n");
    scanf("%d",&potato);
    totalPrice = milk+rice+potato;
    printf("Total Price = %d",totalPrice);
    return 0;
}

Output


[wpusb]