ALGORITHMS


Introduction :

A sequential solution of any program that written in human language, called algorithm.

Algorithm is first step of the solution process, after the analysis of problem, programmers write the algorithm of that problem.

Qualities of a good algorithm:

§  Inputs and outputs should be defined precisely.

§  Each steps in algorithm should be clear and unambiguous.

§  Algorithm should be most effective among many different ways to solve a problem.

§  An algorithm shouldn’t have computer code. Instead, the algorithm should be written in such a way that, it can be used in similar programming languages.

Example of Algorithms:

Write an algorithm to find out number is odd or even?

1.      Start

2.      Input number

3.      rem: = number mod 2

4.      if rem=0 then

print “number even”

else

print “number odd”

endif

5.      Stop


Write an algorithm to add two numbers

1.      Start

2.      Declare variables num1, num2 and sum.

3.      Read values num1 and num2.

4.      Add num1 and num2 and assign the result to sum.

sum := num1 + num2

5.      Display sum

6.      Stop


Write an algorithm to find the largest among three different numbers entered by user.

1.      Start

2.      Declare variables a, b and c.

3.      Read variables a, b and c.

4.      If a > b

If a>c

Display a.

Else

Display c.

Else

If b>c

Display b.

Else

Display c.

5.      Stop


Write an algorithm to find the factorial of a number entered by user.

1.      Start

2.      Declare variables n, factorial and i.

3.      Initialize variables

factorial = 1

i = 1

4.      Read value of n

5.      Repeat the steps until i = n

factorial := factorial * i

i := i + 1

6.      Display factorial

7.      Stop


Write an algorithm to find the Area of Rectangle.

1.      Start

2.      Declare variables l, b, rec.

3.      Read values of a, b.

4.      rec: = l * b

5.      Display rec

6.      Stop


Write an algorithm to find the Area of Circle.

1.      Start

2.      Declare variables r, pi, area.

3.      Initialize variable pi=3.14

4.      Read value of r.

5.      area := pi * r * r

6.      Display area

7.      Stop


Write an algorithm to find the area of Triangle.

1.      Start

2.      Declare variables b, h, area.

3.      Read values of b and h.

4.      area := 1/2 * b * h

5.      Display area

6.      Stop


Write an algorithm to check whether a number entered by user is prime or not.

1.      Start

2.      Declare variables n,i,flag.

3.      Initialize variables

flag := 1

i := 2

4.      Read n from user.

5.      Repeat the steps until i<(n/2)>

If remainder of n/i equals 0

flag := 0

Go to step 6

i := i+1

6.      If flag = 0

Display n is not prime

else

Display n is prime

7.      Stop


Write an algorithm to find the Fibonacci series till term <= 100.

1.      Start

2.      Declare variables a, b and temp.

3.      Initialize variables a := 0, 
b := 1

4.      Display a and b

5.      Repeat steps until b <= 100 

temp := b

b := b + a 

a := temp 

Display b

6.      Stop


Write an algorithm to find the given number is armstrong or not.

1.      Start

2.      Declare variables num, sum, n, rem, mod

3.      Initialize variable sum = 0

4.      Assigning the num value to n 

n = num

5.      Repeat steps untill n >= 1

rem := n % 10

mod := rem * rem * rem

sum := sum + mod

n = n/10

6.      if num value equals sum

print “Number is armstrong

else

print “Number is not armstrong

7.      Stop


Write an algorithm to reverse a number.

1.      Start

2.      Input: num

3.      Initialize reverse = 0

4.      Check whether num>0 then goto step 5

else goto step 9

5.      reverse := reverse * 10

6.      reverse := reverse + num % 10

7.      num := num /10

8.      Goto step 4

9.      Print reverse

10.  Stop


Write an algorithm of swapping two numbers.

1.      Start

2.      Declare variables a, b, temp

3.      Input: a and b

4.      Inter change the values

temp := a

a := b

b := temp

5.      Display a and b.

6.      Stop

Advertisements

Give A message for us

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s