SlideShare a Scribd company logo
Data Structures and Algorithms
Exercise 1


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




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 1: Solution


                1.   Accept a number from the user. Name it as num.
                2.   Declare an integer variable i.
                3.   Assign a value 2 to i.
                4.   Repeat until i > num/2:
                     a. If num % i = 0:
                          i. Display “The number is not prime”
                          ii. Exit
                     b. i = i + 1
                5. Display “The number is prime”.




     Ver. 1.0                                                         Session 2
Data Structures and Algorithms
Exercise 2


                Write an algorithm to generate the first 10 prime numbers.




     Ver. 1.0                                                        Session 2
Data Structures and Algorithms
Exercise 2: Solution


                1. Declare an integer variable, count, and assign the value 0 to
                       it.
                2. Declare an integer variable, num, and assign the value 2 to
                   it.
                3. Repeat until count becomes equal to 10:
                    a. If num is a prime number:
                        i. Display num
                        ii. Set count = count + 1
                    b. Set num = num +1




     Ver. 1.0                                                            Session 2
Data Structures and Algorithms
Exercise 3


                Write an algorithm to accept a number between 1 and 9 and
                display a pattern. For example, if the number entered is 5,
                the following pattern should be displayed:

                1
                2    1
                3    2   1
                4    3   2   1
                5    4   3   2    1




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 3: Solution


                •   Accept a number in the range of 1 - 9 from the user. Store it
                    in a variable, num.
                •   Declare an integer variable, i.
                •   Set i = 1.
                •   Repeat until i becomes greater than num: // To display
                                                                      // num
                    rows
                     a.   Declare an integer variable j
                     b.   Set j = i
                     c.   Display j and insert a space
                     d.   Set j = j – 1
                     e.   If j > 0 go to step c
                     f.   Insert a line break
                     g.   Set i = i + 1

     Ver. 1.0                                                              Session 2
Data Structures and Algorithms
Exercise 4


                Write an algorithm to accept a number between 1 and 9 and
                display a pyramid. For example, if the number entered is 5,
                the following pyramid will be displayed:

                                     1
                                 1   2    1
                             1   2   3    2   1
                         1   2   3   4    3   2   1
                    1    2   3   4   5    4   3   2    1




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 4: Solution


                1. Accept a number from the user in the range of 1-9. Store it
                   in a variable, n.
                2. Set i = 1.
                3. Repeat until i becomes greater than n:
                    •   Set j = 0
                    •   Insert a space
                    •   Set j = j + 1
                    •   If j < n – i, go to step b  // To display n – i spaces
                    •   Set j = 1
                    •   Display the value of j and insert a space
                    •   Set j = j + 1
                    •   If j < i, then go to step f // To display numbers from 1 to i
                    •   Set j = i – 1


     Ver. 1.0                                                                 Session 2
Data Structures and Algorithms
Exercise 4: Solution (Contd.)


                •   Display the value of j and insert a space
                •   Set j = j – 1
                •   If j > 0, go to step j // To display numbers from i – 1 down
                                           // to 1
                •   Give a line break
                •   Set i = i + 1




     Ver. 1.0                                                              Session 2
Data Structures and Algorithms
Exercise 5


                Write an algorithm to accept two strings and check whether
                the second string exists within the first string. For example,
                if the first string is “concatenation” and the second string is
                “cat”, the algorithm should display “Substring found at
                position 4 in the string”. However, if the first string is
                “concatenation” and the second string is “tent”, the algorithm
                should display “Substring not found in the string”.




     Ver. 1.0                                                           Session 2
Data Structures and Algorithms
Exercise 5: Solution


                1. Accept a string value from the user. Store it in a variable, str.
                2. Accept the substring to be searched in str. Store it in a
                   variable, substr.
                3. Store the length of str in an integer variable, len1.
                4. Store the length of substr in an integer variable, len2.
                5. Set i = 0.
                6. Repeat until i becomes equal to len1:
                    •   If str[i] != substr[0], go to step b else go to step d
                        // To find the first matching character
                    •   Set i = i + 1
                    •   If (i < len1) go to step a
                    •   If i = len1, go to step e, else go to step g
                    •   Display “Substring not found in the string”
                    •   Exit

     Ver. 1.0                                                                    Session 2
Data Structures and Algorithms
Exercise 5: Solution (Contd.)


                •   Set j = i     // First character matched. Now match the
                                  // remaining characters
                •   Set k = 0
                •   If str[j] = substr[k], go to step j, else go to step m
                •   Set j = j + 1
                •   Set k = k + 1
                •   If j < len1 and k < len2, go to step i
                •   If k = len2, go to step n, else go to step p
                •   Display “Substring found at position ” + (i + 1) + “ in the string”
                •   Exit
                •   Set i = i + 1




     Ver. 1.0                                                                   Session 2
Data Structures and Algorithms
Exercise 6


                Suppose you have two arrays of size 10 each containing
                elements in ascending order. Write an algorithm to merge
                the two arrays in such a way that the elements in the
                resulting array are arranged in the ascending order.




     Ver. 1.0                                                       Session 2
Data Structures and Algorithms
Exercise 6: Solution


                •   Accept two arrays, A1 and A2, each of size 10, from the
                    user.
                •   Declare an array, result, of size 20.
                •   Set i = j = k = 0.
                •   If (A1 [i] <= A2 [j]): // Insert the smaller element in the
                                           // result array
                     a. result [k] = A1 [i]
                     b. Set k = k + 1
                     c. Set i = i + 1
                •   If (A1[i] > A2[j]):       // Insert the smaller element in the
                                              // result array
                     a. result [k] = A2 [j]
                     b. Set k = k + 1
                     c. Set j = j + 1

     Ver. 1.0                                                                 Session 2
Data Structures and Algorithms
Exercise 6: Solution (Contd.)


                •   If (i < 10 and j < 10), go to step 4.   // If none of the lists
                                                            // has reached its
                    end
                •   Repeat until i equals 10: // If there are any elements left
                                                       // in A1, copy them to
                    result                                      // array

                     a. result [k] = A1 [i]
                     b. Set k = k + 1
                     c. Set i = i + 1
                •   Repeat until j equals 10: // If there are any elements left in
                                             // A2, copy them to the result
                                                       // array

                     a. result [k] = A2 [j]
     Ver. 1.0        b. Set k = k + 1                                        Session 2
Data Structures and Algorithms
Exercise 7


                Write an algorithm to find the Highest Common Factor
                (HCF) of three numbers.




     Ver. 1.0                                                      Session 2
Data Structures and Algorithms
Exercise 7: Solution


                1. Accept three numbers from the user. Store them in
                   variables num1, num2, and num3.
                2. Declare an integer variable, min.
                3. Assign the value of the smallest number among num1,
                   num2, and num3 to min by executing the following steps:
                    a. Set min = num1
                    b. If (num2 < min), set min = num2
                    c. If (num3 < min), set min = num3
                4. Declare an integer variable i.
                5. Set i = min.
                6. If ( num1 % i = 0 and num2 % i = 0 and num3 % i = 0 ):
                    •   Display i      // If i divides all the numbers, then HCF is i
                    •   Exit


     Ver. 1.0                                                                Session 2
Data Structures and Algorithms
Exercise 7: Solution (Contd.)


                1. Set i = i – 1.
                2. Go to step 6.




     Ver. 1.0                       Session 2
Data Structures and Algorithms
Exercise 8


                Write an algorithm to multiply two 3 × 3 matrices.




     Ver. 1.0                                                        Session 2
Data Structures and Algorithms
Algorithm 8: Solution


                1. Declare two 3 × 3 arrays, m1 and m2.
                2. Accept the elements of the two matrices and store them in
                    m1 and m2.
                3. Declare a 3 × 3 matrix, result, to store the result of
                    multiplication.
                4. Set i = 0.
                5. Set j = 0.
                6. Set result [i, j] = 0.
                7. Set k = 0.
                8. result [i, j] + = m1 [i, k] × m2 [k, j].
                9. Set k = k + 1.
                10. If k < 3, go to step 8.
                11. Set j = j + 1.

     Ver. 1.0                                                         Session 2
Data Structures and Algorithms
Algorithm 8: Solution (Contd.)


                1.   If j < 3, go to step 6.
                2.   Set i = i + 1.
                3.   If i < 3, go to step 5.
                4.   Display result.




     Ver. 1.0                                  Session 2
Data Structures and Algorithms
Algorithm 9


                Write a recursive algorithm to print the first n numbers in the
                Fibonacci series.




     Ver. 1.0                                                           Session 2
Data Structures and Algorithms
Algorithm 9: Solution


                Algorithm: Fibo (n)
                2. If n = 1, return 0
                3. If n = 2, return 1
                4. Return (Fibo (n – 1) + Fibo (n – 2))




     Ver. 1.0                                             Session 2

More Related Content

PDF
Sure interview algorithm-1103
PDF
Lec 9 05_sept [compatibility mode]
PPTX
Icom4015 lecture4-f16
PPTX
Icom4015 lecture4-f17
PPTX
Icom4015 lecture12-s16
PPT
13slide graphics
PPTX
Icom4015 lecture3-f17
PPTX
1.2 matlab numerical data
Sure interview algorithm-1103
Lec 9 05_sept [compatibility mode]
Icom4015 lecture4-f16
Icom4015 lecture4-f17
Icom4015 lecture12-s16
13slide graphics
Icom4015 lecture3-f17
1.2 matlab numerical data

What's hot (19)

PPTX
Icom4015 lecture13-f16
PPTX
Icom4015 lecture3-s18
PDF
C sharp chap6
PPTX
20101017 program analysis_for_security_livshits_lecture02_compilers
PDF
Lec 8 03_sept [compatibility mode]
PDF
Algorithm chapter 1
PPTX
CIIC 4010 Chapter 1 f17
PPTX
Java Foundations: Basic Syntax, Conditions, Loops
PDF
C sharp chap5
PPTX
Algorithm Design and Complexity - Course 1&2
PDF
C sharp chap4
PDF
Daa q.paper
PPTX
Icom4015 lecture8-f16
PPTX
Introduction to Neural Netwoks
PDF
Mlp trainning algorithm
PPTX
The Mathematics of RSA Encryption
PPTX
Icom4015 lecture3-f17
PPTX
Icom4015 lecture15-f16
Icom4015 lecture13-f16
Icom4015 lecture3-s18
C sharp chap6
20101017 program analysis_for_security_livshits_lecture02_compilers
Lec 8 03_sept [compatibility mode]
Algorithm chapter 1
CIIC 4010 Chapter 1 f17
Java Foundations: Basic Syntax, Conditions, Loops
C sharp chap5
Algorithm Design and Complexity - Course 1&2
C sharp chap4
Daa q.paper
Icom4015 lecture8-f16
Introduction to Neural Netwoks
Mlp trainning algorithm
The Mathematics of RSA Encryption
Icom4015 lecture3-f17
Icom4015 lecture15-f16
Ad

Viewers also liked (16)

PPS
01 ds and algorithm session_01
PPS
Ds 1
PPS
03 ds and algorithm session_04
PPS
Learn C
PPS
05 ds and algorithm session_07
PPTX
Flowchart and algorithm
PPTX
Python Programming Language
PPTX
Python PPT
PPT
Introduction to Python
PPTX
Algorithms
PDF
Writing algorithms
PPTX
Introduction to Pseudocode
PPTX
Algorithm and flowchart2010
PPTX
Algorithms and Flowcharts
PPTX
Algorithm and flowchart
PPT
Introduction to Python
01 ds and algorithm session_01
Ds 1
03 ds and algorithm session_04
Learn C
05 ds and algorithm session_07
Flowchart and algorithm
Python Programming Language
Python PPT
Introduction to Python
Algorithms
Writing algorithms
Introduction to Pseudocode
Algorithm and flowchart2010
Algorithms and Flowcharts
Algorithm and flowchart
Introduction to Python
Ad

Similar to 02 ds and algorithm session_02 (20)

PDF
Anu DAA i1t unit
PDF
chapter1.pdf ......................................
PDF
01 Notes Introduction Analysis of Algorithms Notes
PDF
2.3 notes b
RTF
Design and Analysis of algorithms
PPTX
designs and analysis of algorithmss.pptx
PPTX
Introduction to Python Programming.pptx
PPS
02 intel v_tune_session_02
PDF
Recitation2IntroductionToPython.pptx.pdf
PPTX
Introduction to python programming ( part-2 )
DOC
LApreC2010-106matrixaprtakehome_4r
PPTX
Java-Programming.forBSITSTUDENTfreespptx
PDF
algorithm
PPT
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
PPTX
Leetcode Session 2 - 2d array in java learn
PDF
Control of flow of the phyton programming
PPT
04-Induction and Recursion.ppt ppt bai tap
PPT
UNIT-2-PPTS-DAA.ppt
PPTX
Programming Exam Help
Anu DAA i1t unit
chapter1.pdf ......................................
01 Notes Introduction Analysis of Algorithms Notes
2.3 notes b
Design and Analysis of algorithms
designs and analysis of algorithmss.pptx
Introduction to Python Programming.pptx
02 intel v_tune_session_02
Recitation2IntroductionToPython.pptx.pdf
Introduction to python programming ( part-2 )
LApreC2010-106matrixaprtakehome_4r
Java-Programming.forBSITSTUDENTfreespptx
algorithm
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Leetcode Session 2 - 2d array in java learn
Control of flow of the phyton programming
04-Induction and Recursion.ppt ppt bai tap
UNIT-2-PPTS-DAA.ppt
Programming Exam Help

More from Niit Care (20)

PPS
Ajs 1 b
PPS
Ajs 4 b
PPS
Ajs 4 a
PPS
Ajs 4 c
PPS
Ajs 3 b
PPS
Ajs 3 a
PPS
Ajs 3 c
PPS
Ajs 2 b
PPS
Ajs 2 a
PPS
Ajs 2 c
PPS
Ajs 1 a
PPS
Ajs 1 c
PPS
Dacj 4 2-c
PPS
Dacj 4 2-b
PPS
Dacj 4 2-a
PPS
Dacj 4 1-c
PPS
Dacj 4 1-b
PPS
Dacj 4 1-a
PPS
Dacj 1-2 b
PPS
Dacj 1-3 c
Ajs 1 b
Ajs 4 b
Ajs 4 a
Ajs 4 c
Ajs 3 b
Ajs 3 a
Ajs 3 c
Ajs 2 b
Ajs 2 a
Ajs 2 c
Ajs 1 a
Ajs 1 c
Dacj 4 2-c
Dacj 4 2-b
Dacj 4 2-a
Dacj 4 1-c
Dacj 4 1-b
Dacj 4 1-a
Dacj 1-2 b
Dacj 1-3 c

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Empathic Computing: Creating Shared Understanding
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
MYSQL Presentation for SQL database connectivity
Empathic Computing: Creating Shared Understanding
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Weekly Chronicles - August'25-Week II
The AUB Centre for AI in Media Proposal.docx
Dropbox Q2 2025 Financial Results & Investor Presentation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

02 ds and algorithm session_02

  • 1. Data Structures and Algorithms Exercise 1 Write an algorithm to check whether a number is a prime number or not. Ver. 1.0 Session 2
  • 2. Data Structures and Algorithms Exercise 1: Solution 1. Accept a number from the user. Name it as num. 2. Declare an integer variable i. 3. Assign a value 2 to i. 4. Repeat until i > num/2: a. If num % i = 0: i. Display “The number is not prime” ii. Exit b. i = i + 1 5. Display “The number is prime”. Ver. 1.0 Session 2
  • 3. Data Structures and Algorithms Exercise 2 Write an algorithm to generate the first 10 prime numbers. Ver. 1.0 Session 2
  • 4. Data Structures and Algorithms Exercise 2: Solution 1. Declare an integer variable, count, and assign the value 0 to it. 2. Declare an integer variable, num, and assign the value 2 to it. 3. Repeat until count becomes equal to 10: a. If num is a prime number: i. Display num ii. Set count = count + 1 b. Set num = num +1 Ver. 1.0 Session 2
  • 5. Data Structures and Algorithms Exercise 3 Write an algorithm to accept a number between 1 and 9 and display a pattern. For example, if the number entered is 5, the following pattern should be displayed: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 Ver. 1.0 Session 2
  • 6. Data Structures and Algorithms Exercise 3: Solution • Accept a number in the range of 1 - 9 from the user. Store it in a variable, num. • Declare an integer variable, i. • Set i = 1. • Repeat until i becomes greater than num: // To display // num rows a. Declare an integer variable j b. Set j = i c. Display j and insert a space d. Set j = j – 1 e. If j > 0 go to step c f. Insert a line break g. Set i = i + 1 Ver. 1.0 Session 2
  • 7. Data Structures and Algorithms Exercise 4 Write an algorithm to accept a number between 1 and 9 and display a pyramid. For example, if the number entered is 5, the following pyramid will be displayed: 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 Ver. 1.0 Session 2
  • 8. Data Structures and Algorithms Exercise 4: Solution 1. Accept a number from the user in the range of 1-9. Store it in a variable, n. 2. Set i = 1. 3. Repeat until i becomes greater than n: • Set j = 0 • Insert a space • Set j = j + 1 • If j < n – i, go to step b // To display n – i spaces • Set j = 1 • Display the value of j and insert a space • Set j = j + 1 • If j < i, then go to step f // To display numbers from 1 to i • Set j = i – 1 Ver. 1.0 Session 2
  • 9. Data Structures and Algorithms Exercise 4: Solution (Contd.) • Display the value of j and insert a space • Set j = j – 1 • If j > 0, go to step j // To display numbers from i – 1 down // to 1 • Give a line break • Set i = i + 1 Ver. 1.0 Session 2
  • 10. Data Structures and Algorithms Exercise 5 Write an algorithm to accept two strings and check whether the second string exists within the first string. For example, if the first string is “concatenation” and the second string is “cat”, the algorithm should display “Substring found at position 4 in the string”. However, if the first string is “concatenation” and the second string is “tent”, the algorithm should display “Substring not found in the string”. Ver. 1.0 Session 2
  • 11. Data Structures and Algorithms Exercise 5: Solution 1. Accept a string value from the user. Store it in a variable, str. 2. Accept the substring to be searched in str. Store it in a variable, substr. 3. Store the length of str in an integer variable, len1. 4. Store the length of substr in an integer variable, len2. 5. Set i = 0. 6. Repeat until i becomes equal to len1: • If str[i] != substr[0], go to step b else go to step d // To find the first matching character • Set i = i + 1 • If (i < len1) go to step a • If i = len1, go to step e, else go to step g • Display “Substring not found in the string” • Exit Ver. 1.0 Session 2
  • 12. Data Structures and Algorithms Exercise 5: Solution (Contd.) • Set j = i // First character matched. Now match the // remaining characters • Set k = 0 • If str[j] = substr[k], go to step j, else go to step m • Set j = j + 1 • Set k = k + 1 • If j < len1 and k < len2, go to step i • If k = len2, go to step n, else go to step p • Display “Substring found at position ” + (i + 1) + “ in the string” • Exit • Set i = i + 1 Ver. 1.0 Session 2
  • 13. Data Structures and Algorithms Exercise 6 Suppose you have two arrays of size 10 each containing elements in ascending order. Write an algorithm to merge the two arrays in such a way that the elements in the resulting array are arranged in the ascending order. Ver. 1.0 Session 2
  • 14. Data Structures and Algorithms Exercise 6: Solution • Accept two arrays, A1 and A2, each of size 10, from the user. • Declare an array, result, of size 20. • Set i = j = k = 0. • If (A1 [i] <= A2 [j]): // Insert the smaller element in the // result array a. result [k] = A1 [i] b. Set k = k + 1 c. Set i = i + 1 • If (A1[i] > A2[j]): // Insert the smaller element in the // result array a. result [k] = A2 [j] b. Set k = k + 1 c. Set j = j + 1 Ver. 1.0 Session 2
  • 15. Data Structures and Algorithms Exercise 6: Solution (Contd.) • If (i < 10 and j < 10), go to step 4. // If none of the lists // has reached its end • Repeat until i equals 10: // If there are any elements left // in A1, copy them to result // array a. result [k] = A1 [i] b. Set k = k + 1 c. Set i = i + 1 • Repeat until j equals 10: // If there are any elements left in // A2, copy them to the result // array a. result [k] = A2 [j] Ver. 1.0 b. Set k = k + 1 Session 2
  • 16. Data Structures and Algorithms Exercise 7 Write an algorithm to find the Highest Common Factor (HCF) of three numbers. Ver. 1.0 Session 2
  • 17. Data Structures and Algorithms Exercise 7: Solution 1. Accept three numbers from the user. Store them in variables num1, num2, and num3. 2. Declare an integer variable, min. 3. Assign the value of the smallest number among num1, num2, and num3 to min by executing the following steps: a. Set min = num1 b. If (num2 < min), set min = num2 c. If (num3 < min), set min = num3 4. Declare an integer variable i. 5. Set i = min. 6. If ( num1 % i = 0 and num2 % i = 0 and num3 % i = 0 ): • Display i // If i divides all the numbers, then HCF is i • Exit Ver. 1.0 Session 2
  • 18. Data Structures and Algorithms Exercise 7: Solution (Contd.) 1. Set i = i – 1. 2. Go to step 6. Ver. 1.0 Session 2
  • 19. Data Structures and Algorithms Exercise 8 Write an algorithm to multiply two 3 × 3 matrices. Ver. 1.0 Session 2
  • 20. Data Structures and Algorithms Algorithm 8: Solution 1. Declare two 3 × 3 arrays, m1 and m2. 2. Accept the elements of the two matrices and store them in m1 and m2. 3. Declare a 3 × 3 matrix, result, to store the result of multiplication. 4. Set i = 0. 5. Set j = 0. 6. Set result [i, j] = 0. 7. Set k = 0. 8. result [i, j] + = m1 [i, k] × m2 [k, j]. 9. Set k = k + 1. 10. If k < 3, go to step 8. 11. Set j = j + 1. Ver. 1.0 Session 2
  • 21. Data Structures and Algorithms Algorithm 8: Solution (Contd.) 1. If j < 3, go to step 6. 2. Set i = i + 1. 3. If i < 3, go to step 5. 4. Display result. Ver. 1.0 Session 2
  • 22. Data Structures and Algorithms Algorithm 9 Write a recursive algorithm to print the first n numbers in the Fibonacci series. Ver. 1.0 Session 2
  • 23. Data Structures and Algorithms Algorithm 9: Solution Algorithm: Fibo (n) 2. If n = 1, return 0 3. If n = 2, return 1 4. Return (Fibo (n – 1) + Fibo (n – 2)) Ver. 1.0 Session 2

Editor's Notes

  • #2: Ask the student to write the algorithm first, and then move to the next slide.
  • #4: Ask the students to write the algorithm first and then move to the next slide.
  • #6: Ask the students to write the algorithm first and then move to the next slide.
  • #8: Ask the students to write the algorithm first and then move to the next slide.
  • #11: Ask the students to write the algorithm first and then move to the next slide.
  • #14: Ask the students to write the algorithm first and then move to the next slide.
  • #17: Ask the students to write the algorithm first and then move to the next slide.
  • #20: Ask the students to write the algorithm first and then move to the next slide.
  • #23: Ask the students to write the algorithm first and then move to the next slide.