Skip to main content

What Is an Algorithm?

 



In this tutorial, we will learn what algorithms are with the help of examples.

In computer programming terms, an algorithm is a set of well-defined instructions to solve a particular problem. It takes a set of input(s) and produces the desired output. For example,

An algorithm to add two numbers:

  1. Take two number inputs

  2. Add numbers using the + operator

  3. Display the result


Qualities of a Good Algorithm

  • Input and output should be defined precisely.
  • Each step in the algorithm should be clear and unambiguous.
  • Algorithms should be most effective among many different ways to solve a problem.
  • An algorithm shouldn't include computer code. Instead, the algorithm should be written in such a way that it can be used in different programming languages.

Algorithm Examples

Algorithm to add two numbers

Algorithm to find the largest among three numbers

Algorithm to find all the roots of the quadratic equation

Algorithm to find the factorial

Algorithm to check prime number

Algorithm of Fibonacci series


Algorithm 1: Add two numbers entered by the user

Step 1: Start
Step 2: Declare variables num1, num2 and sum. 
Step 3: Read values num1 and num2. 
Step 4: Add num1 and num2 and assign the result to sum.
        sum←num1+num2 
Step 5: Display sum 
Step 6: Stop

Algorithm 2: Find the largest number among three numbers

Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
           If a > c
              Display a is the largest number.
           Else
              Display c is the largest number.
        Else
           If b > c
              Display b is the largest number.
           Else
              Display c is the greatest number.  
Step 5: Stop

Algorithm 3: Find Roots of a Quadratic Equation ax2 + bx + c = 0

Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
         D ← b2-4ac
Step 4: If D ≥ 0
              r1 ← (-b+√D)/2a
              r2 ← (-b-√D)/2a 
              Display r1 and r2 as roots.
        Else     
              Calculate real part and imaginary part
              rp ← -b/2a
              ip ← √(-D)/2a
              Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop             

Algorithm 4: Find the factorial of a number

Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
          factorial ← 1
          i ← 1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
     5.1: factorial ← factorial*i
     5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop

Algorithm 5: Check whether a number is prime or not

Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
        flag ← 1
        i ← 2  
Step 4: Read n from the user.
Step 5: Repeat the steps until i=(n/2)
     5.1 If remainder of n÷i equals 0
            flag ← 0
            Go to step 6
     5.2 i ← i+1
Step 6: If flag = 0
           Display n is not prime
        else
           Display n is prime
Step 7: Stop 

Algorithm 6: Find the Fibonacci series till the term less than 1000

Step 1: Start 
Step 2: Declare variables first_term,second_term and temp. 
Step 3: Initialize variables first_term ← 0 second_term ← 1 
Step 4: Display first_term and second_term 
Step 5: Repeat the steps until second_term ≤ 1000 
     5.1: temp ← second_term 
     5.2: second_term ← second_term + first_term 
     5.3: first_term ← temp 
     5.4: Display second_term 
Step 6: Stop

Comments

Popular posts from this blog

For Sale: Established Tech Blog with 200k+ Views

Are you looking for a prime opportunity to own a successful tech blog? We are offering our well-established blog, which focuses on cutting-edge AI technologies and innovative startups. Here’s why this could be your next great investment: Why This Blog? Impressive Traffic : With over 200k views, this blog has built a substantial and engaged audience. Focused Niche : Specializes in AI and tech startups, making it a valuable resource for tech enthusiasts and professionals. Current Revenue : Currently earning from a sponsored post, showcasing its potential to generate income. Growth Potential : Significant opportunities for increased revenue through additional ads, sponsorships, and expanded content. Key Features Established Platform : A reliable and respected blog with a loyal readership. Content Quality : High-quality, relevant content that attracts and retains readers. Revenue Opportunities : Room for growth in monetization through diversified advertising and sponsorship strategies. Wha...

Kotlin (programming language)

  If you have heard of Kotlin it’s because you are interested in Android development. Kotlin is one of the programming languages that has gained the most momentum over the last 5 years. In fact,  over 80% of the 1000 most popular Android apps currently use Kotlin. What you may not know about Kotlin is that is not only used for Android  app development  but has also been making its way into other sectors. Keep reading if you want to know more about Kotlin and the advantages of using this programming language. 1  What is Kotlin? 1.1  Kotlin’s History 2  Kotlin’s Features and Benefits 2.1  Interoperability with Java Code 2.2  Easy Learning Curve 2.3  Reduced Programming Time 2.4  Object-Oriented and Functional Programming 2.5  Coroutines 2.6  Cross-Platform Development 2.7  Flexibility 3  Kotlin vs. Java 4  Kotlin Testing What is Kotlin? Kotlin is an open-source programming language created by JetBrains that ha...

5 Essential CSS Tricks for Beginners

Welcome to TechBitu! If you're just starting out with web development, mastering CSS (Cascading Style Sheets) is crucial for creating visually appealing and responsive websites. In this guide, we'll cover five essential CSS tricks that every beginner should know. These tips will help you enhance your web design skills and create better user experiences. Let’s dive in! ### Table of Contents 1. [Centering with Flexbox](#centering-with-flexbox) 2. [Responsive Images](#responsive-images) 3. [Box Shadows](#box-shadows) 4. [Custom Fonts](#custom-fonts) 5. [Hover Effects](#hover-effects) ### 1. Centering with Flexbox One of the most common challenges in web design is centering elements both vertically and horizontally. Flexbox makes this task straightforward and efficient. ```css .container {     display: flex;     justify-content: center;     align-items: center;     height: 100vh; } ``` In this example, the `justify-content: center;` centers the child ...