Algorithm
Definition 1
Algorithm is a set of instructions in a sequence to accomplish a particular task/problem.
Definition 2
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a
certain order to get the desired output.
Algorithms are generally created independent of underlying languages, i.e. an algorithm can be
implemented in more than one programming language.
Characteristics of Algorithm
1.Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one meaning.
2.Input − An algorithm should have 0 or more well-defined inputs.
3.Output − An algorithm should have 1 or more well-defined outputs, and should match the desired
output.
4.Finiteness − Algorithms must terminate after a finite number of steps.
5.Feasibility − Should be feasible with the available resources.
6.Independent − An algorithm should have step-by-step directions, which should be independent of any
programming code.
1.Algorithm to add two numbers
Step 1 Start.
Step 2 Accept Number one.
Step 3 Accept Number two.
Step 4 Add both the numbers.
Step 5 Store the result
Step 6 Print the result.
Step 7 Stop
2.Algorithm for area of circle
1.start
2.accept radius r
3.calculate area=3.14*r*r
4.display area
5.stop
3.Algorithm to multiply two numbers
Step 1.Start
Step 2.Accept number 1
Step 3.aceept number 2
Step 4.Multiply both numbers
Step 5.Store multiplication
Step 6.Display multiplication.
Step 7.Stop.
4.Algorithm to check number is positive or negative.
Step 1.Start
Step 2 Accept number
Step 3.check if number >0
Step 4.Display number is Positive.
Go to step 6
Step 5. Else Display number is Negative
Step 6.stop.
5.Algorithm to check number is even or odd.
Step 1 : Start.
Start 2 : Accept number n.
Start 3 : if (n%2 == 0).
Display number is even
else
Display number is odd
Start 4 : Stop
6.Algorithm to find maximum number between 2 numbers.
Step 1.Start
Step 2.Accept no1.
Step 3.Accept no2.
Step 4.check if no1>no2
Display no1 is maximum
else
Display no2 is maximum.
Step 5.Stop
7.Algorithm to find maximum number between 3 numbers.
Step 1.Start
Step 2.Accept no1, no2, no3
Step 3.if no1 > no2 goto step 4,
otherwise goto step 5
Step 4.if no1 > no3 goto step 6,
otherwise goto step 8
Step 5.if no2 > no3 goto step 7, otherwise goto step 8
Step 6.Display"no1 is the largest",
And goto step 9
Step 7. Display "no2 is the largest",
And goto step 9
Step 8. Display " no3 is the largest"
Step 9.Stop
8.Algorithm to find sum of first 'n' natural numbers.
Step 1.Start
Step 2. Read/Accept the value of n.
Step 3. i = 1 , SUM = 0
Step 4. if ( i <= n ) go to step 5
else go to 8
Step 5. SUM = SUM + i.
Step 6. i = i + 1.
Step 7. go to 4
Step 8. Display the value of SUM
Step 9. Stop
9.Algorithm to find sum of digits of given numbers. (input=123,output=6).
Step 1.Start.
Step2. Accept num
Step 3.sum=0, rem=0
Step 4.Get the modulus/remainder of the number i.e rem=num%10
Step 5.sum the remainder of the number
i.e.sum=sum+rem
Step 6 . Divide the number by 10.
i.e.num=num/10.
Step 7. Repeat from the step 4 while number is greater than 0.
Step 8.diplay Sum
Step 9. Stop
10.Algorithm to reverse number (i/p=1234,o/p=4321)
Step 1. Start
Step 2. Initialize rev=0
Step 3. Accept num.
Step 4.check if num>0 then goto step 5
else go to step 9
Step 5. rev=rev*10
Step 6. rev=rev+num%10
Step 7. num=num/10
Step 8. Goto step 4
Step 9. Display rev
Step 10.Stop
11.Algorithm for generating table of any number
Step 1: Start
Step 2: Initialize i=1
Step 3: Read/accept a number,i.e. num
Step 4: Repeat step 5 until i=10 reach
Step 5: Print num*i
Step 6.increment i by 1 goto step 4
Step 7: Stop
12.Algorithm for calculating factorial of any number
step 1. Start
step 2. Read/accept the number n
step 3. Initialize i=1,fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i.
step 6. i=i+1
step 7. Print fact
step 8. Stop
13.Algorithm to check given number is prime number or not.
Prime Number- The number which is divisible only by itself and 1is known as prime
number.i.e.it does not have any factors.
Step 1: Start
Step 2: Read number n 5
Step 3: Set f=0
Step 4: For i=2 to n-1
Step 5: If n % i=0 then
Step 6: Set f=1 and break
Step 7: go to step 4
Step 8: If f=0 then
print 'The given number is prime'
else
print 'The given number is not prime'
Step 9: Stop
14.Algorithm to check given number is perfect number or not.
Perfect Number- Perfect number is a number which is equal to sum of its divisor. ... For eg,
divisors of 6 are 1,2 and 3.
Step 1. Start
Step 2. Accept number n
Step 3.set i = 1,rem=0, sum=0
Step 4.repeat while loop till (i < n).
Step 5.Divide n by i. i.e. n/i
Step 6.If the remainder is 0, add the value to “sum” variable.
Step 7.Increment the variable ” i “.
Step 8.If the “sum” is equal to “i “,
Display Number is perfect
else display number is not perfect.
Step 9.Stop
15.Algorithm to check given number is armstrong number or not.
Armstrong Member-If we get sum of cube of digits in number as that number against then the
number is know as armstrong number.
Step 1.Start
Step 2.read number
Step 3.set sum=0 and duplicate=number
Step 4.re=number%10
Step 5.sum=sum+(rem*rem*rem)
Step 6.number=number/10
Step 7.repeat steps 4 to 6 until number > 0
Step 8.if sum = duplicate
display number is armstrong
else
display number is not armstrong
Step 9.stop
16.Algorithm to check given number is palimdrome number or not.
palimdrome number- If we get the same number after reversing it then that number is known
as palimdrome number.
Step 1.start
Step 2.get a number(x)
Step 3.set p=0 and temp=x
Step 4.divide x by 10, store reminder (x%10) in r
Step 5.set x=x/10
Step 6.set p= p + r
Step 7.set p=p*10
Step 8.repeat from step 4 till x is equal to 0
Step 9. set p = p /10
Step 10. check if p is equal to temp
Step 11. if so, print palindrome
Step 12. else print not a palindrome
Step 13. Stop