Showing posts with label algorithm in programming. Show all posts
Showing posts with label algorithm in programming. Show all posts

Wednesday, 23 September 2020

algorithm | algorithm in programming | algorithm in c - Online help

In this post, we are going to discuss in detail what is an algorithm in programming and what are all the basic factors that should follow or consider while writing an algorithm to solve a problem. So let's get started.

algorithm in programming

 Algorithm in programming:

An algorithm is a set of English-like statements, which is used to illustrate the logic of the program. The word algorithm is named after the Arabian mathematician Musa al Khowarismi, which means method, or recipe, or procedure.

The algorithm defines the logic of solving a problem and hence it should be organized properly, in such a way it is meaningful and unambiguous. The solution for the problem should be defined precisely and all the statements should be stated correctly and explicitly.


Factors to follow while writing an algorithm:

The following are the various factors that should be considered before writing an algorithm to solve the problem:
  • The nature of the problem should be clearly understood by defining the problem specification.
  • The various output that should be obtained.
  • The inputs available for obtaining the output.
  • The sequence of steps, known as an algorithm, to achieve the result, should be clearly written down.
  • The program should be tested for the different values and known results to find the errors, which may exist.
  • Debug the error if any.
  • Document the program to make the users understand the program better.


algorithm tutorial:

Let's see an algorithm tutorial to write an algorithm to find the sum of the series 1+2+3+..........+10.

Step 1: Initialize sum=0

Step 2: Initialize num=1

Step 3: sum=sum+num

Step 4: num=num+1

Step 5: if num<=10 then go to step 3

Step 6: print sum

Step 7: stop

In the above algorithm, we have to set an initial value of 0 to the variable 'sum'. This process is called initialization.

Again in step 2, we have initialized the value of num to 1. In step 3, the value of 'sum' is added to the value of 'num' and the resultant value is stored in the variable 'sum'. This is called an assignment. Note that the left-hand side of the expression is a variable and the right-hand side of the expression is substituted by the value of the variable.

so the value of the 'sum' will be equal to 1. Next in step 4, the value of the 'num' is set to num+1, that is, the value 1 is added to the existing value of 'num', and the resultant value is stored in the variable 'num'. This process is called incrementing the value. Initially, the value of 'num' was 1. After processing step 4,  the value of 'num' will be equal to 2.

In step 5, we check whether the 'num' is less than or equal to 10. If this condition fails then the control is taken back to step 3. If the condition is true then step 6 is executed to print the value of 'sum'.


Basic algorithm for beginners: 

Let's discuss the process of writing an algorithm by choosing some of the day-to-day activities of our life.

Example 1: Write an algorithm to prepare a cup of coffee.

Step 1: take a container and pour a cup of milk.

Step 2: switch on the stove.

Step 3: place the container on the stove.

Step 4: is the milk boiling? if not, continue heating the milk.

Step 5: if the milk boils, then add one teaspoon of coffee powder to the container.

Step 6: Add two teaspoons of sugar to the container.

Step 7: stir the container with a spoon

Step 8: does sugar and coffee powder dissolve?

Step 9: if not, go to step 7.

Step 10: switch off the stove.

Step 11: pour the coffee into a cup

Step 12: serve or drink the coffee




Example 2:  Write an algorithm to go to your college by bus.

Step 1: Start from home and reach the bus stop.

Step 2: wait for the desired bus to come.

Step 3: if the bus has come, check whether it is the required bus.

Step 4: if not, go to step 2.

Step 5: board the bus.

Step 6: purchase the ticket.

Step 7: travel until your college stop is reached.

Step 8: Check whether your college stop has come.

Step 9: if not, go to step 7.

Step 10: get down from the bus.

Step 11: walk and reach your college.


Example 3: Write an algorithm to accept your name and age and print it.

Step 1: accept name

Step 2: accept age

Step 3: print "your name is:", name," and your age is:", age

Step 4: stop


Also read:  Javascript function


Example 4: Write an algorithm to two numbers and print the largest of two.

Step 1: accept a,b

Step 2: if a>b then print a "is greater" : go to step 4

Step 3: otherwise print b "is greater"

Step 4: stop

I hope you guys understand what is an algorithm in programming if you have any queries or you find any mistake then please comment below. Thank you so much for reading my article.