Nexus Learn

Practical 11: Loop-Based Problem Solving

Lab Practical CS-307 Programming Fundamentals 6 min read Updated 8 Nov 2026
On this page
  1. 1. Lab Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A: Warm-up Trace (3 min)
  6. Part B: Predict the Output (4 min)
  7. Part C: Bug Hunt (5 min)
  8. Part D: Graded Lab Tasks (32 min · 10 marks)
  9. Part E: Post-Lab Questions (finish as homework if needed)
  10. Marking Rubric (10 marks)
  11. Submission

Duration

Language

Tools

Linked Lecture

50 minutes

C++

Dev-C++ or Code::Blocks (online compiler as backup)

Lectures 14 and 16 (and 11–15)

Mapped outcomes: CLO1 (problem solving) and CLO4 (C++ syntax and semantics) at psychomotor level P3 – Guided Response · PLO2, PLO3, PLO5

1. Lab Objectives

By the end of this 50-minute lab, you will be able to:

  • Plan a loop program with top-down design before coding.
  • Work with the digits of a number using % 10 and / 10.
  • Write prime and palindrome checks with the flag and search patterns.
  • Test programs with edge cases and fix flag, copy and limit bugs.

2. Word Bank

Word

Simple meaning

Real-world example

Digit

One place of a number (0–9)

The digits of a mobile PIN

Prime

A number ≥ 2 divisible only by 1 and itself

7 students can only sit in 1 row of 7 or 7 rows of 1

Palindrome

Reads the same both ways

The number 1221 or the word 'level'

Flag

A bool that records whether something happened

A green / red light at a door

Edge case

An unusual input at the limit

Entering 0 or 1 in a calculator

3. Background

Reminder

int t = n; // work on a copy

do {

int d = t % 10; // last digit

t /= 10; // remove it

} while (t > 0);

bool isPrime = (n >= 2);

for (int d = 2; d * d <= n; d++)

if (n % d == 0) { isPrime = false; break; }

4. Time Plan (50 minutes)

Part

Activity

Time

—

Briefing

2 min

A

Warm-up trace

3 min

B

Predict the output

4 min

C

Bug hunt (3 bugs)

5 min

D

Graded tasks 1–3

32 min

E

Post-lab questions & submission

4 min

Part A: Warm-up Trace (3 min)

lab11_warmup.cpp

#include <iostream>

using namespace std;

int main() {

int n = 382, sum = 0, rev = 0;

while (n > 0) {

int d = n % 10;

sum += d;

rev = rev * 10 + d;

n /= 10;

}

cout << sum << " " << rev << endl;

return 0;

}

Trace table (n, d, sum, rev) and output

Part B: Predict the Output (4 min)

lab11_predict.cpp

#include <iostream>

using namespace std;

int main() {

int count = 0;

for (int d = 1; d <= 12; d++) {

if (12 % d == 0) count++;

}

cout << "Divisors of 12: " << count << endl;

int a = 1, b = 1;

while (b < 50) {

int next = a + b;

a = b;

b = next;

}

cout << "First term over 50: " << b << endl;

return 0;

}

My prediction

Part C: Bug Hunt (5 min)

bug1.cpp — flag not reset

#include <iostream>

using namespace std;

// Should print every prime from 2 to 30

int main() {

bool isPrime = true;

for (int n = 2; n <= 30; n++) {

for (int d = 2; d * d <= n; d++) {

if (n % d == 0) isPrime = false;

}

if (isPrime) cout << n << " ";

}

cout << endl;

return 0;

}

Wrong output, cause and fix

bug2.cpp — original destroyed

#include <iostream>

using namespace std;

// Should say whether n is a palindrome

int main() {

int n, rev = 0;

cin >> n;

while (n > 0) {

rev = rev * 10 + n % 10;

n /= 10;

}

if (rev == n) cout << "Palindrome" << endl;

else cout << "Not a palindrome" << endl;

return 0;

}

Wrong output, cause and fix

bug3.cpp — wrong limit

#include <iostream>

using namespace std;

// Should print the perfect numbers up to 500 (6 28 496)

int main() {

for (int n = 2; n <= 500; n++) {

int sum = 0;

for (int d = 1; d <= n; d++) {

if (n % d == 0) sum += d;

}

if (sum == n) cout << n << " ";

}

cout << "(end)" << endl;

return 0;

}

Wrong output, cause and fix

Part D: Graded Lab Tasks (32 min · 10 marks)

Rules

Write the top-down steps as comments before coding.

Test each program with at least one edge case (0, 1, a negative number).

Save as lab11_task1.cpp, lab11_task2.cpp, lab11_task3.cpp.

Task 1: Digit Report (Easy — 3 marks)

Read a whole number (0 or more; validate with do-while). Print the number of digits, the sum of digits, the largest digit and the number reversed. 0 must be reported as 1 digit.

Sample run

Enter a whole number (0 or more): -5

Enter a whole number (0 or more): 90210

Digits : 5

Sum of digits : 12

Largest digit : 9

Reversed : 1209

Task 2: Primes in a Range (Medium — 3 marks)

Read a start value (1 or more) and an end value (not less than the start), both validated. Print all primes in the range, then their count and sum. If there are none, print a message.

Sample run 1

From (1 or more): 0

From (1 or more): 10

To (10 or more): 5

To (10 or more): 30

11 13 17 19 23 29

Count: 6, sum: 112

Sample run 2 (edge case)

From (1 or more): 24

To (24 or more): 28

No primes in this range.

Task 3: Number Checker (Challenge — 4 marks)

Read numbers until 0 (sentinel). Skip negative numbers with a message (use continue). For each positive number print whether it is prime and whether it is a palindrome. At the end print how many numbers were checked and how many were prime and palindromes.

Sample run

Enter positive numbers (0 to stop):

131 -4 7 22 1 0

131: prime, palindrome

skipped (negative)

7: prime, palindrome

22: not prime, palindrome

1: not prime, palindrome

Checked 4 numbers: 2 prime, 4 palindrome

Part E: Post-Lab Questions (finish as homework if needed)

Q1. Why does Task 1 use a do-while instead of a while loop?

Answer

Q2. In Task 2, why is the prime test only run while d * d <= n?

Answer

Q3. Write three edge cases you used to test Task 3 and the expected output for each.

Answer

Marking Rubric (10 marks)

Component

Marks

What earns full marks

Task 1

3

Validated input; correct count, sum, largest and reverse (0 → 1 digit).

Task 2

3

Validated range; correct primes, count and sum; message when none.

Task 3

4

Sentinel loop with continue; prime and palindrome checks; correct totals.

Parts A–C and the post-lab questions are checked in the viva. A program that does not compile gets at most half marks for that task.

Submission

  • Show your running programs to the instructor before leaving.
  • Copy the final code and outputs into your Lab Book (Practical 11 pages).
  • Copying code from a classmate gets zero marks for both students.

Next practical: Practical 12 — Arrays I (Week 7).

← All CS-307 material