Lab 4 – Programming Practice
C Programming – 10 Unique Tasks
C Task 1: Grid Matrix of Custom Characters
Question: Write a C program that outputs a two-dimensional grid matrix of custom characters. Prompt the user for number of rows R, number of columns C, and a character symbol (e.g. ‘$’). Use nested for loops (outer loop for rows, inner loop for columns) to display the RxC grid.
Solution:
#include <stdio.h>
int main() {
int rows, cols;
char symbol;
printf("Enter number of rows (R): ");
scanf("%d", &rows);
printf("Enter number of columns (C): ");
scanf("%d", &cols);
printf("Enter character symbol to print: ");
scanf(" %c", &symbol);
printf("
--- Custom Grid Matrix (%dx%d) ---
", rows, cols);
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++) {
printf("%c ", symbol);
}
printf("
");
}
return 0;
}
Concept Explanation:
This beginner program demonstrates two-dimensional rectangular grid generation using nested for loops from Lab 4. The outer for loop controls horizontal row traversal while the inner for loop prints individual column characters across each row.
C Task 2: Increasing Number Triangle
Question: Write a C program that displays an increasing number triangle pattern. Prompt the user for total rows N. Use nested for loops where row i contains numbers from 1 up to i (e.g. row 1: “1”, row 2: “1 2”, row 3: “1 2 3”).
Solution:
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("
--- Increasing Number Triangle ---
");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("
");
}
return 0;
}
Concept Explanation:
This beginner task practices nested for loop triangle patterns from Lab 4. The inner loop iteration limit is dynamically bounded by the current outer loop row index i (j <= i), producing an expanding triangular pattern.
C Task 3: School Classroom Desk Labeler
Question: Write a C program that simulates multi-classroom desk inspection. Suppose a school has 2 classrooms, and each classroom contains 4 desks. Use nested while loops (outer while for classroom number, inner while for desk number) to print detailed desk audit labels.
Solution:
#include <stdio.h>
int main() {
int classroom = 1;
int totalClassrooms = 2;
int desksPerClassroom = 4;
printf("=== SCHOOL DESK INSPECTION LOG ===
");
while (classroom <= totalClassrooms) {
printf("Classroom %d:
", classroom);
int desk = 1;
while (desk <= desksPerClassroom) {
printf(" Desk #%d in Classroom %d verified.
", desk, classroom);
desk++;
}
classroom++;
}
return 0;
}
Concept Explanation:
This beginner program demonstrates nested while loops for multi-tiered structural tasks as explained in Lab 4. The outer while loop steps through top-level classroom groups while the inner while loop iterates sub-level desk items.
C Task 4: Solid Square Star Pattern
Question: Write a C program that displays a solid square pattern of asterisks (*). Prompt the user to enter side length N. Use nested for loops to display an NxN square grid of stars.
Solution:
#include <stdio.h>
int main() {
int side;
printf("Enter side length of square (N): ");
scanf("%d", &side);
printf("
--- Solid Star Square Pattern ---
");
for (int i = 1; i <= side; i++) {
for (int j = 1; j <= side; j++) {
printf("* ");
}
printf("
");
}
return 0;
}
Concept Explanation:
This beginner task demonstrates simple 2D square pattern rendering using nested for loops from Lab 4. Outer loop i controls row iteration while inner loop j outputs star symbols across columns.
C Task 5: Inverted Star Triangle Pattern
Question: Write a C program to print an inverted right-angled star triangle pattern. Prompt for starting row count N. Use nested for loops where outer loop i decrements from N down to 1, and inner loop j prints i stars per row.
Solution:
#include <stdio.h>
int main() {
int rows;
printf("Enter starting row count (N): ");
scanf("%d", &rows);
printf("
--- Inverted Star Triangle ---
");
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("
");
}
return 0;
}
Concept Explanation:
This beginner task practices decremental nested for loops from Lab 4. The outer loop counter i decreases on each iteration, causing the inner column loop to render progressively shorter star lines.
C Task 6: Floyd’s Number Triangle Generator
Question: Write a C program to generate Floyd’s Triangle pattern up to N rows. Floyd’s Triangle displays consecutive natural numbers in a right triangle layout (Row 1: 1, Row 2: 2 3, Row 3: 4 5 6…). Use nested for loops with a continuous incrementing counter variable.
Solution:
#include <stdio.h>
int main() {
int rows, count = 1;
printf("Enter number of rows for Floyd's Triangle: ");
scanf("%d", &rows);
printf("
--- Floyd's Number Triangle ---
");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", count);
count++;
}
printf("
");
}
return 0;
}
Concept Explanation:
This medium program demonstrates nested for loops combined with a persistent counter variable from Lab 4. The continuous counter variable persists across inner loop runs, generating sequential integers arranged in a triangle structure.
C Task 7: Factory Production Shift Inspector
Question: Write a C program that simulates automated factory machinery audits. The factory operates 2 work shifts, with 3 assembly machines per shift. Use nested do-while loops (outer do-while for shifts, inner do-while for machines) to display operational readiness check logs.
Solution:
#include <stdio.h>
int main() {
int shift = 1;
int totalShifts = 2;
int totalMachines = 3;
printf("=== FACTORY MACHINERY READINESS LOG ===
");
do {
printf("Work Shift %d Inspection:
", shift);
int machine = 1;
do {
printf(" Machine #%d on Shift %d: Operational OK.
", machine, shift);
machine++;
} while (machine <= totalMachines);
shift++;
} while (shift <= totalShifts);
return 0;
}
Concept Explanation:
This medium program practices nested do-while loops from Lab 4 for layered group inspections. Both outer shift loop and inner machine loop execute at least once, guaranteeing systematic coverage of multi-tiered factory levels.
C Task 8: Multiplication Table Grid from 1 to N
Question: Write a C program to output a full multiplication table matrix for values from 1 to N. Prompt the user for grid dimension N. Use nested for loops to calculate cell products (i * j) and print a neatly formatted multiplication grid.
Solution:
#include <stdio.h>
int main() {
int n;
printf("Enter multiplication table limit (N): ");
scanf("%d", &n);
printf("
=== MULTIPLICATION TABLE GRID (1 to %d) ===
", n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%4d", i * j);
}
printf("
");
}
return 0;
}
Concept Explanation:
This medium program demonstrates two-dimensional matrix calculation using nested for loops from Lab 4. The inner loop evaluates cell arithmetic (i * j) while width format specifiers (%4d) align columns into clean grid tables.
C Task 9: Full Diamond Star Pattern Generator
Question: Write a C program that displays a full symmetric diamond star pattern. Prompt the user for half-height N. Use two sequential sets of nested for loops: the first set generates an upright pyramid (1 to N), and the second set generates an inverted pyramid (N-1 down to 1).
Solution:
#include <stdio.h>
int main() {
int n;
printf("Enter half-height N for diamond: ");
scanf("%d", &n);
printf("
--- Full Symmetric Diamond Star Pattern ---
");
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) {
printf(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
printf("*");
}
printf("
");
}
for (int i = n - 1; i >= 1; i--) {
for (int s = 1; s <= n - i; s++) {
printf(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
printf("*");
}
printf("
");
}
return 0;
}
Concept Explanation:
This challenging task applies multi-stage nested loop structures from Lab 4. It combines an expanding upright pyramid nested loop block followed by a contracting inverted pyramid nested loop block to form a diamond shape.
C Task 10: Multi-City Temperature Reading Analyzer
Question: Write a C program to analyze weather data across multiple cities. Suppose there are 3 cities, and 4 daily temperature readings are recorded per city. Use nested while loops to input readings, calculate total and average temperature per city, and output city summary reports.
Solution:
#include <stdio.h>
int main() {
int city = 1;
int totalCities = 3;
int readingsPerCity = 4;
printf("=== MULTI-CITY WEATHER DATA ANALYZER ===
");
while (city <= totalCities) {
printf("
City #%d Data Entry:
", city);
int reading = 1;
double sum = 0.0, temp;
while (reading <= readingsPerCity) {
printf(" Enter Temperature Reading #%d (°C): ", reading);
scanf("%lf", &temp);
sum += temp;
reading++;
}
double avg = sum / readingsPerCity;
printf("City #%d Summary: Total = %.1lf°C, Average Temp = %.2lf°C
", city, sum, avg);
city++;
}
return 0;
}
Concept Explanation:
This challenging problem demonstrates nested while loops combined with cumulative data aggregation from Lab 4. The outer city loop resets sum accumulators while the inner reading loop gathers sub-level readings and calculates city averages.
C++ Programming – 10 Unique Tasks
C++ Task 1: Solid Square Box Pattern
Question: Write a C++ program that generates a solid square box pattern of hash symbols (#). Prompt the user for side length N. Use nested for loops (outer for rows, inner for columns) to output an NxN grid box of # symbols.
Solution:
#include <iostream>
using namespace std;
int main() {
int side;
cout << "Enter side length of square box (N): ";
cin >> side;
cout << "
--- Solid Square Box Pattern (" << side << "x" << side << ") ---" << endl;
for (int i = 1; i <= side; i++) {
for (int j = 1; j <= side; j++) {
cout << "# ";
}
cout << endl;
}
return 0;
}
Concept Explanation:
This beginner program introduces two-dimensional square grid generation in C++ using nested for loops from Lab 4. Outer loop i handles vertical row iteration while inner loop j handles horizontal column character printing.
C++ Task 2: Repeated Row Number Triangle
Question: Write a C++ program that prints a right-angled triangle pattern where each row repeats its row number. Prompt for total rows N. Use nested for loops where row i prints row number i i times (e.g. row 1: “1”, row 2: “2 2”, row 3: “3 3 3”).
Solution:
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
cout << "
--- Repeated Row Number Triangle ---" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
Concept Explanation:
This beginner program practices nested for loop triangle patterns in C++ from Lab 4. The inner loop runs i times for row i and outputs the outer loop index variable i to repeat the row index value across columns.
C++ Task 3: Parking Lot Section Inspector
Question: Write a C++ program that simulates automated parking garage slot verification. The garage has 3 parking zones, and each zone contains 3 parking slots. Use nested while loops (outer while for zone, inner while for slot) to log inspection progress.
Solution:
#include <iostream>
using namespace std;
int main() {
int zone = 1;
const int totalZones = 3;
const int slotsPerZone = 3;
cout << "=== PARKING GARAGE SLOT AUDIT ===" << endl;
while (zone <= totalZones) {
cout << "Zone " << zone << " Inspection:" << endl;
int slot = 1;
while (slot <= slotsPerZone) {
cout << " Zone " << zone << " - Slot #" << slot << " verified." << endl;
slot++;
}
zone++;
}
return 0;
}
Concept Explanation:
This beginner task practices nested while loops in C++ for two-tier hierarchical tasks from Lab 4. The outer while loop increments parking zone counters while the inner while loop steps through individual parking slots.
C++ Task 4: Inverted Symbol Pyramid Pattern
Question: Write a C++ program that prints an inverted right triangle pattern of stars (*). Prompt for total rows N. Use nested for loops where outer loop i steps from N down to 1, and inner loop j prints i stars per row.
Solution:
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter starting number of rows (N): ";
cin >> rows;
cout << "
--- Inverted Symbol Pyramid ---" << endl;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Concept Explanation:
This beginner program demonstrates decremental nested for loops in C++ from Lab 4. Decrementing outer counter i shrinks inner loop column bounds, rendering a top-heavy inverted triangle shape.
C++ Task 5: Rectangle Symbol Grid Matrix
Question: Write a C++ program to display a rectangular grid pattern of plus symbols (+). Prompt the user for number of rows R and columns C. Use nested for loops to display an RxC grid of + symbols.
Solution:
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter number of rows (R): ";
cin >> rows;
cout << "Enter number of columns (C): ";
cin >> cols;
cout << "
--- Rectangle Plus Grid Matrix ---" << endl;
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++) {
cout << "+ ";
}
cout << endl;
}
return 0;
}
Concept Explanation:
This beginner task demonstrates two-dimensional rectangular matrix rendering using nested for loops in C++ from Lab 4. The outer loop traverses rows while the inner loop prints column symbols.
C++ Task 6: Continuous Number Matrix Grid
Question: Write a C++ program that generates a continuous numbering matrix grid. Prompt for number of rows R and columns C. Use nested for loops with a persistent counter variable starting at 1 that increments continuously across grid cells, printing values separated by tabs.
Solution:
#include <iostream>
using namespace std;
int main() {
int rows, cols, count = 1;
cout << "Enter number of matrix rows (R): ";
cin >> rows;
cout << "Enter number of matrix columns (C): ";
cin >> cols;
cout << "
--- Continuous Number Matrix Grid ---" << endl;
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++) {
cout << count << " ";
count++;
}
cout << endl;
}
return 0;
}
Concept Explanation:
This medium task practices matrix grid population with global counting state in C++ from Lab 4. The count variable increments sequentially across all matrix cell iterations to form an ordered numerical grid.
C++ Task 7: Library Bookshelf Inventory Audit
Question: Write a C++ program that simulates library inventory auditing. Suppose a library has 3 bookcase units, and each bookcase unit has 2 shelves. Use nested do-while loops (outer do-while for bookcases, inner do-while for shelves) to display audit verification logs.
Solution:
#include <iostream>
using namespace std;
int main() {
int bookcase = 1;
const int totalBookcases = 3;
const int totalShelves = 2;
cout << "=== LIBRARY BOOKSHELF AUDIT LOG ===" << endl;
do {
cout << "Bookcase Unit " << bookcase << " Audit:" << endl;
int shelf = 1;
do {
cout << " Shelf " << shelf << " of Bookcase " << bookcase << " verified." << endl;
shelf++;
} while (shelf <= totalShelves);
bookcase++;
} while (bookcase <= totalBookcases);
return 0;
}
Concept Explanation:
This medium program demonstrates nested do-while loops in C++ from Lab 4 for layered group inspections. Both bookcase and shelf loops execute their verification statements at least once before testing condition bounds.
C++ Task 8: Addition Table Grid Generator
Question: Write a C++ program to generate an addition table grid for numbers 1 to N. Prompt the user for limit N. Use nested for loops to calculate cell sums (i + j) and display a two-dimensional addition matrix grid with tab spacing.
Solution:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter addition grid dimension limit (N): ";
cin >> n;
cout << "
=== ADDITION TABLE GRID (1 to " << n << ") ===" << endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << (i + j) << " ";
}
cout << endl;
}
return 0;
}
Concept Explanation:
This medium program practices 2D mathematical table generation using nested for loops in C++ based on Lab 4. The inner loop evaluates cell addition expressions (i + j) to build addition matrix layouts.
C++ Task 9: Hollow Square Box Pattern
Question: Write a C++ program to display a hollow square box pattern of stars (*). Prompt for side length N. Use nested for loops and an if-else statement to print stars only on the borders (i == 1 || i == N || j == 1 || j == N) and spaces in the interior.
Solution:
#include <iostream>
using namespace std;
int main() {
int side;
cout << "Enter side length of hollow box (N): ";
cin >> side;
cout << "
--- Hollow Square Box Pattern (" << side << "x" << side << ") ---" << endl;
for (int i = 1; i <= side; i++) {
for (int j = 1; j <= side; j++) {
if (i == 1 || i == side || j == 1 || j == side) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
Concept Explanation:
This challenging program combines nested for loops with conditional boundary checks in C++ from Lab 4. It prints stars on outer row and column perimeters while leaving interior cells hollow with blank spaces.
C++ Task 10: Multi-Student Test Grade Analyzer
Question: Write a C++ program to process test scores for a class. Suppose there are 3 students, and each student has completed 3 test assignments. Use nested for loops to input assignment scores per student, compute total and average score per student, and display performance reports.
Solution:
#include <iostream>
using namespace std;
int main() {
const int totalStudents = 3;
const int testsPerStudent = 3;
cout << "=== MULTI-STUDENT GRADE PERFORMANCE ANALYZER ===" << endl;
for (int student = 1; student <= totalStudents; student++) {
cout << "
Student #" << student << " Score Entry:" << endl;
double sum = 0.0, score;
for (int test = 1; test <= testsPerStudent; test++) {
cout << " Enter Test #" << test << " Score (out of 100): ";
cin >> score;
sum += score;
}
double avg = sum / testsPerStudent;
cout << "Student #" << student << " Summary: Total Score = " << sum
<< " / 300, Average Score = " << avg << " %" << endl;
}
return 0;
}
Concept Explanation:
This challenging problem demonstrates nested for loops with student accumulator tracking in C++ from Lab 4. The outer student loop resets total sum metrics while the inner test loop gathers sub-level test scores and computes averages.