Looking For Anything Specific?

ads header

What is an algorithm?-With example.

What is an algorithm?-With example. 



    Definition of Algorithm: 

    •       An algorithm can be defined as the finite sequence of steps that can be performed in the sequence to get the desired result. 

    •       A sequential solution to any problem written in human language is called the Algorithm.

    •        An algorithm provides a blueprint for writing a program to solve a particular problem. 

      Before learning the process of writing an algorithm lets understand the characteristics of an algorithm. 

    What is an algorithm?



    Characteristic of Algorithm: 

    • Input: An algorithm should have 0 or more well defined inputs .

    • Output: An algorithm should have at least 1 well defined output, or it may be more than 1.

    • Unambiguous: An algorithm should be clear and must lead to only one meaning. 

    • Finiteness: An algorithm must terminate after finite number of steps. 

    • Feasibility: An algorithm should be feasible with the available resources. 

    • Independent: An algorithm should have step-by-step directions which should be independent of any  programming code.  



    Key Features of Algorithm: 

    1. Sequence: 


    Sequence means that each step of the algorithm is executed in the specified order. An algorithm performs the steps in sequential order. 


    Eg.  visit and read blog article on this website.


    Step:1 open any browser. 

    Step:2 search www.compileyourcode.blogspot.com

    Step:3 read the blog. 

    • An algorithm to add two numbers is as given below. 

    Step 1: Input the first number as A 

    Step 2: Input the Second number as B 

    Step 3: Set Sum = A + B 

    Step 4: Print Sum 

    Step 5: End

    2. Decision: 


        Decision statements are used when the outcome of the process depends on some condition.


    Eg. IF age >= 18 

    then Eligible for voting 

    Else 

    Not Eligible for voting 

    For example, if X = Y, then print “Equal”. So the general form of if construct can be if condition  then process 


    A condition is any statement that may evaluate either to a true value or a false value.


    if condition 

    then process1 

    else  process2 


    This form is commonly known as if-else construct. Here, if the condition is true then process1 is  executed, else process2 is executed. 

    • An algorithm to check equality of two numbers: 

    Step 1: Input the first number as A 

    Step 2: Input the second number as B 

    Step 3: if A = B 

    then Print “Equal” 

    else 

    Print “Not Equal” 

    Step 4: End 

    3. Repetition: 


    Repetition involves executing one or more processes for a number of times. 


    Eg. Fill Water 

    Until the glass is full 

    Eg. Go to college and attend Lectures and Labs 

    Repeat step 1 Until it’s a Holiday 


    Repetition can be implemented using constructs such as while, do-while and for loops. This  loops execute one or more steps until some condition is true. 

    • Write an algorithm to print first 10 natural numbers. 

    Step 1: [initialize] Set I = 1, N = 10 

    Step 2: Print I, Set I = I + 1

    Step 3: Repeat Step 2 while I <= N 

    Step 4: End 

    •    Write an algorithm for interchanging/swapping two values

    Step 1: Input the first number as A 

    Step 2: Input the second number as B 

    Step 3: Set temp = A 

    Step 4: Set A = B 

    Step 5: Set B = temp 

    Step 6: Print A, B 

    Step 7: End 

    • Write an algorithm to find larger of two numbers

    Step 1: Input the first number as A 

    Step 2: Input the second number as B 

    Step 3: if A > B 

    then Print “A is Larger” 

    else if A < B 

    then Print “B is Larger” 

    else  

    Print “The Numbers are Equal” 

    Step 4: End 


    • Write an algorithm to find largest among three numbers.

    Step 1: Input the numbers as A, B, C 


    Step 2: if A > B and A > C 

    then Print “A is Largest ” 

    else if B > A and B > C 

    then Print “B is Largest” 

    else  

    Print “C is Largest” 

    Step 3: End 


    • Write an algorithm to find whether the number is odd or even.

    Step 1: Input the first number as A 
    Step 2: if A % 2 = 0 
    then Print “Even” 

    else 

    Print “Odd” 

    Step 3: End

    • Write an algorithm to print grade obtained by a student using following rules:

                       Marks Grade 

                                     75 and Above O 
                                     60-<75 A 
                                     50-<60 B 
                                     40-<50 C 
                                     Less than 40 D 

    Step 1: Enter the marks obtained as M 

    Step 2: if M >= 75 

    then Print “O” 

    Step 3: if M >= 60 and M < 75 

    then Print “A” 

    Step 4: if M >= 50 and M < 60 

    then Print “B” 

    Step 5: if M >=40 and M < 50 

    then Print “C” 

     else 

    Print “D” 

    Step 6: End 

    • Write an algorithm to print first 10 natural numbers. 

    Step 1: Set Set I = 1, N = 10 

    Step 2: Print I, Set I = I + 1 

    Step 3: Repeat Step 2 while I <= N 

    Step 4: End 


    • Write an algorithm to find sum of first N natural numbers.

    Step 1:Input N 
    Step 2: Set I = 1, sum = 0 
    Step 3: Set sum = sum + I 
    Set I = I + 1 
    Step 4: Repeat Step 3 while I <= N 
    Step 5: Print sum 
    Step 6: End 
    • Write an algorithm to calculate simple interest. 

    Step 1: START 

    Step 2: Read P, T, R 

    Step 3: Calculate SI=P*R*T/100 

    Step 4: PRINT SI 

    Step 5: STOP

       

      Now you clear all the concepts of Algorithm.


    Post a Comment

    0 Comments