How to write Algorithms

How to write Algorithms for solving problems?

Algorithm:

An algorithm is the set of rules that are needed to be followed while solving problems. Here we'll learn how to write algorithms for the given simple problems.

Tips for writing an algorithm :

  • Read the problem carefully.
  • Don't start writing the code immediately. It is always best to start with the algorithm.
  • After understanding the problem, think about the logic required to solve the problem.
  • Once you root out the logic behind the problem, then start writing the algorithm which is nothing but the procedures of solving the problem in a step wise manner.


Problem 1:
To check whether the given number is positive or negative.
Input: A number
Output: Positive or negative
Procedure:
Step 1 : Start
Step 2 : Read the number(n) from the user.
Step 3 : If the number is less than zero, print "negative".
If the number is greater than zero, print "positive".
Step 4 : End

Problem 2:
To check whether the given number is odd or even.
Input: A number
Output: Odd or Even
Procedure:
Step 1: Start
Step 2: Read the number (n) from the user
Step 3: Divide n by 2
                     1. If the remainder is zero, print  "Even"
                     2.Otherwise, print "Odd"
Step 4 : End 

Problem 3:
To find the Area of circle.
Input: Radius (r)
Output : Area (A)
Process:
Step 1 :Start
Step 2 :Get radius (r) from the user
Step 3 :Calculate Area, A=π*r*r
Step 4 :Print A
Step 5 :End

Problem 4:
To print how may minutes and seconds are present in the given seconds.
Input : Seconds (S)
Output: Minutes (min) & seconds (sec)
Process:
Step 1 :Start
Step 2 :Get seconds (S) from the user
Step 3 :Divide seconds by 60
Step 4 :Print quotient as min & remainder as sec
Step 5 :End

Problem 5:
To print number and it's square.
Input: A number
Output: Number and it's square
Process:
Step 1 :Start
Step 2 :Get number (n) from the user
Step 3 : Calculate Square , S=n*n
Step 4 :Print S
Step 5 : End

Problem 6 :
To compute (x+y)/(x-y) for two numbers x and y.
Input : Two numbers x and y
Output : C=(x+y)/(x-y)
Process:
Step 1 : Start
Step 2 :Get numbers x and y from the user
Step 3 :Calculate C=(x+y)/(x-y)
Step 4 :Print C
Step 5 :End

Problem 7:
To find n + nn + nnn for given number n
Input : A number n
Output : X=n+nn+nnn
Process:
Step 1 :Start
Step 2 :Get number n from the user
Step 3 :Calculate S= n + nn + nnn
Step 4 :Print S
Step 5 :End


Comments

Popular posts from this blog

Finding the percentage in python - HackerRank solution

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

What's your name, Tuple - HackerRank solution in python