Introduction of Algorithm

Introduction of Algorithm

In this blog, we are going to see an introduction to algorithms.

What is an algorithm?

The algorithm is not only used in Programming and Technologies. We all use algorithms in our daily activities. For instance, You are going to start a car.

  1. First you open the car and get inside.

  2. Place the key on the keyhole.

  3. After turning the key, the car gets started.

  4. Here, you follow these steps to start the car. Similarly, Algorithm is a set of instructions to solve a specific problem.

Why do we need an Algorithm?

The algorithm is a pre-planning base for tasks. The algorithm prevents unnecessary mistakes and errors while we solve the task.

For instance, we build a building without any plan. If we need to change anything, we should destroy the previously build and then build newly. we use an Algorithm to avoid this kind of issue. If we plan before building a building, we can avoid waste of money and waste of time. Once any changes we need to add, first we apply the changes in the algorithm then we should apply those changes in our program.

How to apply the algorithm in the program?

We discuss this question with a perfect example.

Algorithm for adding two numbers.

  1. START

  2. Declare two integer variables number1 and number2.

  3. Take the two numbers to add, as inputs in variables number1 and number2 respectively.

  4. Declare an integer variable sum to store the resultant sum of the two numbers.

  5. Add the two numbers and store the result in the variable sum.

  6. Print the value of the variable sum.

  7. END

Program:

C

#include <stdio.h>
int main() {    

    int number1, number2, sum;

    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    // calculating sum
    sum = number1 + number2;      

    printf("Sum=%d",sum);
    return 0;
}

C++

#include <iostream>
using namespace std;

int main() {

  int number1, number2, sum;

  cout << "Enter two integers: ";
  cin >> number1 >> number2;

  // sum of two numbers in stored in variable sumOfTwoNumbers
  sum = number1 + number2;

  // prints sum 
  cout <<  "Sum = " <<  sum;     

  return 0;
}

Python

# Store input 
number1 = int(input('Enter first number: '))
number2 = int(input('Enter second number: '))
sum = number1 + number2
# Print the sum
print('Sum = ',sum)

Java

import java.util.Scanner;

public class SumOfTwoNumbersScanner {

 public static void main(String[] args) {

  // reading input from user
  Scanner scanner = new Scanner(System.in);
  System.out.print("Enter First Number : ");
  int number1 = scanner.nextInt();
  System.out.print("Enter Second Number : ");
  int number2 = scanner.nextInt();

  // summing two numbers
  int sum = number1 + number2;

  System.out.println("Sum = " +  sum);

 }

}

If you enjoyed this article, share it with your friends and colleagues! and we will discuss more algorithms in the upcoming blog........

"An algorithm must be seen to be believed.

-Donald Knuth"