CSC1103: Fundamentals of Programming [ C ]
Week 6
Fall 2024
Sheikh Usama Khalid
Department of Computer Science
100 Building, Clifton, Room 301
[email protected]
Lecture Plan
• Chapter 6: Loops
Fundamentals of Programming
• Programming Review
Fundamentals of Programming
Chapter 6: Loops
Topic: Nested Loops
Fundamentals of Programming
Topic: Nested Loops
Programming Review
Nested Loop for a Rectangular Pattern
#include <stdio.h>
int main() {
int rows = 4;
int columns = 5;
int i,j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= columns; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
The outer loop controls the number of rows, and the inner loop controls the number of
columns in each row.
Nested Loop for a Right-Angled Triangle
Pattern
#include <stdio.h>
int main() {
int rows = 5, i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
The outer loop controls the number of rows, and the inner loop controls the number of
columns in each row.
Nested Loop for Multiplication Table
#include <stdio.h>
int main() {
int n = 5,i,j;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= n; j++) {
printf("%d x %d = %d\t", j, i, i * j);
}
printf("\n");
}
return 0;
}
The outer loop controls the number of rows, and the inner loop controls the number of
columns in each row.