================================================================================ INTRODUCTION TO HIGH-LEVEL PROGRAMMING (C++) PRACTICAL LAB SESSION 4: Loops (for, while, do-while) State University of Zanzibar (SUZA) ================================================================================ OBJECTIVES: - Master for, while, and do-while loops - Convert between loop types - Use nested loops for patterns and tables - Implement counting and accumulation patterns ================================================================================ PART A: BASIC LOOPS (From Course Materials) ================================================================================ 1. Loop Conversion: Given this while loop, rewrite it as a for loop that produces the same output: int sum = 0, i = 0; while (i < 5) { sum += i; i++; } cout << sum << endl; 2. Write a program to display all odd numbers between 0 and 1000. 3. Write a program to display the sum of all even numbers between 0 and 100. ================================================================================ PART B: COUNTING & ACCUMULATION ================================================================================ 4. Factorial Calculator: Read a positive integer n. Calculate and display n! Test: 5! = 120 | 0! = 1 | 10! = 3628800 5. Multiplication Table: Read a number n. Display its multiplication table from 1 to 12. Example for n=7: 7 x 1 = 7 7 x 2 = 14 ... 7 x 12 = 84 6. Number Guessing Game: Generate a random number between 1 and 100 (use rand()). Let the user guess. After each guess, say "Too high" or "Too low". Count and display the number of guesses when they get it right. 7. Sum Until Sentinel: Read numbers from the user until they enter -1. Display the count, sum, and average of the entered numbers. (Do not include -1 in calculations) 8. Fibonacci Sequence: Read n. Display the first n Fibonacci numbers. Test: n=10 -> 0 1 1 2 3 5 8 13 21 34 9. Power of 2 Table: Display all powers of 2 from 2^0 to 2^20. Format: 2^0 = 1 2^1 = 2 2^2 = 4 ... 2^20 = 1048576 ================================================================================ PART C: NESTED LOOPS & PATTERNS ================================================================================ 10. Full Multiplication Table (1 to 10): Display a formatted 10x10 multiplication table. 11. Star Patterns - Write programs to display each: Pattern A (Right Triangle): * ** *** **** ***** Pattern B (Inverted Right Triangle): ***** **** *** ** * Pattern C (Pyramid): * *** ***** ******* ********* Pattern D (Diamond): * *** ***** *** * Pattern E (Number Triangle): 1 12 123 1234 12345 12. Hollow Rectangle: Read rows and columns. Print a hollow rectangle of stars. Example (4x6): ****** * * * * ****** ================================================================================ PART D: EXTRA PRACTICE (LeetCode / HackerRank Style) ================================================================================ 13. [Easy] Count Digits Read a positive integer. Count how many digits it has using a loop. Test: 12345 -> 5 | 100 -> 3 | 7 -> 1 14. [Easy] Sum of Digits Read a positive integer. Calculate the sum of its digits. Test: 1234 -> 10 | 999 -> 27 | 100 -> 1 15. [Easy] Reverse a Number Read an integer. Print it reversed using a loop. Test: 12345 -> 54321 | 1000 -> 1 16. [Easy] Check Prime Number Read a number. Determine if it is prime. Test: 7 -> Prime | 12 -> Not prime | 2 -> Prime | 1 -> Not prime 17. [Easy] GCD (Greatest Common Divisor) Read two numbers. Find their GCD using Euclid's algorithm. Test: GCD(48, 18) = 6 | GCD(100, 75) = 25 18. [Medium] Print All Primes up to N Read N. Print all prime numbers from 2 to N. Test: N=20 -> 2 3 5 7 11 13 17 19 19. [Medium] Armstrong Number Check A number is Armstrong if sum of cubes of digits equals the number. 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 -> Armstrong Test: 153 -> Yes | 370 -> Yes | 123 -> No 20. [Medium] Perfect Number Check A perfect number equals the sum of its divisors (excluding itself). Test: 6 (1+2+3=6) -> Perfect | 28 (1+2+4+7+14=28) -> Perfect | 12 -> Not 21. [HackerRank] Staircase Print a right-aligned staircase of height n using # symbols. Link: hackerrank.com/challenges/staircase Test n=4: # ## ### #### 22. [LeetCode 202] Happy Number A happy number: replace with sum of squares of digits, repeat. If it reaches 1, it's happy. If it loops endlessly, it's not. Test: 19 -> Happy (19->82->68->100->1) Test: 2 -> Not happy 23. [LeetCode 1281] Subtract Product and Sum of Digits Given integer n, return difference between product of digits and sum. Test: 234 -> 24 - 9 = 15 | 4421 -> 32 - 11 = 21 24. [Medium] Collatz Conjecture Start with number n. If even, divide by 2. If odd, multiply by 3 and add 1. Print each step until you reach 1. Count the steps. Test: 6 -> 6, 3, 10, 5, 16, 8, 4, 2, 1 (8 steps) ================================================================================