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

Blockchain developer roadmap

 # The Ultimate Blockchain Developer Roadmap: A Comprehensive Guide Blockchain technology is revolutionizing industries, from finance to supply chain management, offering a secure, decentralized way to record transactions and manage data. As a blockchain developer, you’ll be at the forefront of this digital revolution. This roadmap is designed to guide you through the essential skills and knowledge you need to become a proficient blockchain developer. ## Understanding the Basics ### What is Blockchain? Blockchain is a distributed ledger technology that allows data to be stored globally on thousands of servers. This makes it decentralized, transparent, and immutable. Each transaction is recorded in a block, and these blocks are linked together to form a chain. ### Why Become a Blockchain Developer? 1. **High Demand**: With the rise of cryptocurrencies and decentralized applications (dApps), blockchain developers are in high demand. 2. **Lucrative Salaries**: Blockchain developers are am

Android developer roadmap

  A skilled Android developer can fix the problems of millions of people. With only one app. But with only  5,9 million Android developers  worldwide there is a huge demand for more.  If you want to be part of the most popular mobile platform in the world there is no better time to start. This blog will guide you through your  android developer roadmap  and help you understand the fundamentals Table of Contents How long does it take to become an Android developer? 1. Choose an IDE for android development 2. Pick a language 3. Get familiar with the anatomy of an app 4. Learn the basics of UI 5. Learn the paradigm of asynchronous programming 6. Explore the fundamentals of Android Jetpack 7. Choose where to store your data 8. Connect your app to the internet 9. Choose the right application architecture 10. Don’t forget to test your code  11. Configure your application build Conclusion How long does it take to become an Android developer? To be honest, it’s going to take a while. However,

5 Proven Strategies to Skyrocket Your Social Media Engagement

"Discover 5 effective strategies to boost your social media engagement and grow your brand's online presence. Learn how to create compelling content, engage with your audience, and more!" Introduction:   In today’s digital age, social media engagement is crucial for building  your brand 's presence and connecting with your audience. High engagement  rates can lead to increased brand loyalty, more followers, and even higher conversion rates. At QuadSocial, we specialize in boosting social media  engagement for brands of all sizes. Here are five proven strategies to   help you get started: 1. Create High-Quality, Relevant Content: The foundation of any successful social media strategy is creating content that resonates with your audience. Focus on producing high-quality visuals, videos, and written posts that provide value and reflect your brand’s voice. Remember to stay relevant to current trends and topics in your industry. 2. Engage with Your Audience: Socia