Slide 2
Quick Recap of Lecture 15
4 minutes · homework check
Homework 3: pyramid
Output (n = 4)
- for (int i = 1; i <= n; i++) {
- for (int s = 1; s <= n - i; s++) cout << " ";
- for (int j = 1; j <= 2 * i - 1; j++) cout << "*";
- cout << endl;
- }
- *
- ***
- *****
- *******
HW4: 5+4+3+2+1 = 15
- Today
- We have all the loop tools. Today we use them to solve real problems — and we test our programs properly.
Recap
Programming Fundamentals · Lecture 16
Slide 3
Today's Plan
50 minutes · 9 parts
Recap & homework check
4 min
Learning outcomes
2 min
Top-down design for loop problems
7 min
Digits of a number
7 min
Prime numbers
9 min
Series, GCD & number checks
8 min
Class activity
6 min
Testing with edge cases & recap
6 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 16
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Break a loop problem into steps with top-down design before coding
SLO 16.1 → CLO1 · C3
Write loop programs for digits, prime numbers, series and number checks
SLO 16.2 → CLO4 · C3
SLO 16.3 → CLO1 · C4
Test loop programs with edge cases and fix the logic errors found
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 16
Slide 5
Top-Down Design for Loop Problems
Solve the big problem by splitting it into small steps
- Step-wise refinement
- 1 For each n from 2 to N
- 2 Is n prime? ← its own loop
- 3 If yes: print n, count++
- 4 Print the count
- Rule
- Write the steps in plain words first. Each step becomes a few lines of code. A step that itself needs repeating becomes an inner loop.
Module 1 · Top-Down Design
Programming Fundamentals · Lecture 16
Slide 6
Working With Digits: % 10 and / 10
n % 10 gives the last digit · n / 10 removes it
digits.cpp (main part)
- int n, count = 0, sum = 0;
- cin >> n;
- do {
- sum += n % 10; // last digit
- count++;
- n = n / 10; // remove last digit
- } while (n > 0);
Session
- Uses
- Digit count, digit sum, reverse, palindrome, Armstrong numbers.
- Enter a number: 4075
- Digits: 4, sum: 16
Module 2 · Digits of a Number
Programming Fundamentals · Lecture 16
Slide 7
Is n Prime? Flag + Search Pattern
A prime has exactly two divisors: 1 and itself
prime.cpp (main part)
- bool isPrime = (n >= 2); // 0 and 1 are not prime
- for (int d = 2; d * d <= n; d++) {
- if (n % d == 0) {
- isPrime = false; // found a divisor
- break;
- }
- }
- Patterns used
- Flag (isPrime)
- Search + break
- Why d * d <= n?
- Every factor pair has one factor ≤ √n. For 97 we test only d = 2 … 9.
Module 3 · Prime Numbers
Programming Fundamentals · Lecture 16
Slide 8
All Primes up to a Limit
The top-down steps become a nested loop: step 2 is the inner loop
primes_list.cpp (main part)
- Reset!
- isPrime = true is inside the outer loop, so each n starts fresh.
- for (int n = 2; n <= limit; n++) { // step 1: every number
- bool isPrime = true;
- for (int d = 2; d * d <= n; d++) { // step 2: test it
- if (n % d == 0) {
- isPrime = false;
- break;
- }
- }
- if (isPrime) { // step 3: print, count
- cout << n << " ";
- count++;
- }
- }
- Here, n starts at 2
- so the flag can start as true.
Output
Primes up to: 50 → 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 Count: 15
Module 3 · Prime Numbers
Programming Fundamentals · Lecture 16
Slide 9
Series and GCD
Keep the last values in variables and update them each round
fibonacci.cpp (main part)
gcd.cpp (main part)
- int a = 0, b = 1;
- for (int i = 1; i <= 10; i++) {
- cout << a << " ";
- int next = a + b;
- a = b;
- b = next;
- }
- int x = a, y = b;
- while (y != 0) { // Euclid's method
- int r = x % y;
- x = y;
- y = r;
- }
- // GCD is x
Output
Session
0 1 1 2 3 5 8 13 21 34
- Two numbers: 48 18
- GCD = 6, LCM = 144
Real-world: GCD cuts a 48 × 18 tile area into the biggest equal squares (6 × 6).
Module 4 · Series, GCD & Number Checks
Programming Fundamentals · Lecture 16
Slide 10
Number Checks: Armstrong Numbers
Digit loop inside a range loop
armstrong.cpp (main part)
- Check 153
- 1³ + 5³ + 3³
- = 1 + 125 + 27
- = 153 ✓
- for (int n = 100; n <= 999; n++) {
- int t = n, sum = 0;
- while (t > 0) {
- int dgt = t % 10;
- sum += dgt * dgt * dgt;
- t /= 10;
- }
- if (sum == n) cout << n << " ";
- }
- Work on a copy
- t = n. If we divided n itself, the outer loop counter would be destroyed.
Output
153 370 371 407
Module 4 · Series, GCD & Number Checks
Programming Fundamentals · Lecture 16
Slide 11
CLASS ACTIVITY · 6 MIN · IN PAIRS
Plan, Code, Predict
Write the steps in words first, then the loop. Predict the output before running.
Tasks
Hints
- 1. Digit count and digit sum of 90210?
- 2. Print Fibonacci terms smaller than 100. How many terms?
- 3. Is 97 prime? How many divisors d does prime.cpp test?
- 1: % 10 and / 10
- 2: while (a < 100)
- 3: d * d <= 97
Slide 12
Testing With Edge Cases
A program that works for 'normal' input can still be wrong
prime_bug.cpp
- bool isPrime = true; // wrong
- // fix: (n >= 2)
- Fixes
- Count digits with do-while.
- Prime flag starts (n >= 2).
- Check for 0 0 before / .
- Edge-case checklist
- 0 · 1 · negative · smallest · largest · equal values · first/last item
Test first, then trust.
Module 5 · Testing
Programming Fundamentals · Lecture 16
Slide 13
Quick Recap
Answer aloud
Q1
Which two operators give and remove the last digit?
Q2
Why do we test divisors only while d * d <= n?
Q3
Which edge cases would you test for a prime checker?
Q4
What are the four top-down questions for a loop problem?
Programming Fundamentals · Lecture 16
Slide 14
Homework
Read a number, print it reversed and say whether it is a palindrome (test 12321 and 1231).
Print all perfect numbers from 1 to 1000 (a perfect number = sum of its divisors below it, e.g. 6 = 1 + 2 + 3).
Print the primes between 50 and 100 and how many there are.
Make an edge-case test table (input, expected, got) for gcd.cpp and fix the crash.
Next lecture: Arrays I — storing many values under one name