0% found this document useful (0 votes)
3 views

Nested Loop Examples

The document provides examples of C++ programs using nested loops to create various menu systems and patterns. It includes programs for displaying sections with students, printing square and triangle patterns, and generating a number pyramid. Each example is accompanied by code snippets and explanations of the output.

Uploaded by

iqraghouri440
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Nested Loop Examples

The document provides examples of C++ programs using nested loops to create various menu systems and patterns. It includes programs for displaying sections with students, printing square and triangle patterns, and generating a number pyramid. Each example is accompanied by code snippets and explanations of the output.

Uploaded by

iqraghouri440
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Nested Menu System

In interactive systems or software, nested loops can generate menus with multiple options and
sub-options.

Example: Displaying a Menu

#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 2; i++) { // Outer loop for main menu
cout << "Main Menu " << i << endl;
for (int j = 1; j <= 3; j++) { // Inner loop for sub-menu
cout << " Sub-Menu " << j << endl;
}
}
return 0;
}

Write a program in C++ to handle the scenario where you have 5 sections, each containing
10 students. We’ll use nested loops to represent the sections and the students.

Program: Sections and Students


#include <iostream>
using namespace std;

int main() {
int totalSections = 5; // Number of sections
int studentsPerSection = 10; // Number of students in each section

for (int section = 1; section <= totalSections; section++) { // Outer


loop for sections
cout << "Section " << section << ":" << endl;

for (int student = 1; student <= studentsPerSection; student++) {


// Inner loop for students
cout << " Student " << student << endl;
}
cout << endl; // Add a blank line after each section for clarity
}

return 0;
}

Question 1: Print a Square Pattern

Write a program to print a square of * of size 5 using nested loops.

*****
*****
*****
*****
*****
#include <iostream>
using namespace std;
int main() {
int n = 5; // Size of the square
for (int i = 0; i < n; i++) { // Outer loop for rows
for (int j = 0; j < n; j++)
{ // Inner loop for columns
cout << "* ";
}
cout << endl; // Move to the next line
}
return 0;
}

Working Table:

Outer Inner Action Output (Partial Complete Output


Loop (i) Loop (j) Performed Row) (After endl)
i = 0 j = 0 Print * *
j = 1 Print * * *
j = 2 Print * * * *
j = 3 Print * * * * *
j = 4 Print * * * * * * * * * * *
i = 1 j = 0 Print * *
j = 1 Print * * *
j = 2 Print * * * *
j = 3 Print * * * * *
j = 4 Print * * * * * * * * * * *
i = 2 j = 0 Print * *
j = 1 Print * * *
j = 2 Print * * * *
j = 3 Print * * * * *
j = 4 Print * * * * * * * * * * *
i = 3 j = 0 Print * *
j = 1 Print * * *
j = 2 Print * * * *
j = 3 Print * * * * *
j = 4 Print * * * * * * * * * * *
i = 4 j = 0 Print * *
j = 1 Print * * *
j = 2 Print * * * *
j = 3 Print * * * * *
j = 4 Print * * * * * * * * * * *

Question 2: Print a Right Triangle Pattern

Write a program to print a right-angled triangle of * with 5 rows.

*
**
***
****
*****
#include <iostream>
using namespace std;

int main() {
int n = 5; // Number of rows
for (int i = 1; i <= n; i++) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for columns
cout << "* ";
}
cout << endl; // Move to the next line
}
return 0;
}

Working Table:

Outer Inner Action Output (Partial Complete Output


Loop (i) Loop (j) Performed Row) (After endl)
i = 1 j = 1 Print * * *
i = 2 j = 1 Print * *
j = 2 Print * * * * *
i = 3 j = 1 Print * *
j = 2 Print * * *
j = 3 Print * * * * * * *
i = 4 j = 1 Print * *
j = 2 Print * * *
j = 3 Print * * * *
j = 4 Print * * * * * * * * *
i = 5 j = 1 Print * *
j = 2 Print * * *
j = 3 Print * * * *
j = 4 Print * * * * *
j = 5 Print * * * * * * * * * * *

Question 3: Inverted Triangle Pattern

Write a program to print an inverted triangle of * with 5 rows.

*****
****
***
**
*
#include <iostream>
using namespace std;

int main() {
int n = 5; // Number of rows
for (int i = n; i >= 1; i--) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for
columns
cout << "* ";
}
cout << endl; // Move to the next line
}
return 0;
}

Working Table:

Outer Inner Action Output (Partial Complete Output


Loop (i) Loop (j) Performed Row) (After endl)
i = 5 j = 1 Print * *
j = 2 Print * * *
j = 3 Print * * * *
j = 4 Print * * * * *
j = 5 Print * * * * * * * * * * *
i = 4 j = 1 Print * *
j = 2 Print * * *
j = 3 Print * * * *
j = 4 Print * * * * * * * * *
i = 3 j = 1 Print * *
j = 2 Print * * *
j = 3 Print * * * * * * *
i = 2 j = 1 Print * *
j = 2 Print * * * * *
i = 1 j = 1 Print * * *

Question 4: Number Pyramid

Write a program to print the following number pyramid for 5 rows:

1
12
123
1234
12345

#include <iostream>
using namespace std;

int main() {
int n = 5; // Number of rows
for (int i = 1; i <= n; i++) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for
columns
cout << j << " ";
}
cout << endl; // Move to the next line
}
return 0;
}

Working Table:

Outer Inner Action Output (Partial Complete Output


Loop (i) Loop (j) Performed Row) (After endl)
i = 1 j = 1 Print 1 1 1
i = 2 j = 1 Print 1 1
j = 2 Print 2 1 2 1 2
i = 3 j = 1 Print 1 1
j = 2 Print 2 1 2
j = 3 Print 3 1 2 3 1 2 3
i = 4 j = 1 Print 1 1
j = 2 Print 2 1 2
j = 3 Print 3 1 2 3
j = 4 Print 4 1 2 3 4 1 2 3 4
i = 5 j = 1 Print 1 1
j = 2 Print 2 1 2
j = 3 Print 3 1 2 3
j = 4 Print 4 1 2 3 4
j = 5 Print 5 1 2 3 4 5 1 2 3 4 5

You might also like