0% found this document useful (0 votes)
10 views3 pages

Printing Rectangle of Stars Using Nested Loops

The document provides C++ code examples for printing patterns using nested loops. It includes a rectangle of stars, a triangle of numbers, and a multiplication table. Each example demonstrates the use of for, while, and do-while loops to manage rows and columns.

Uploaded by

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

Printing Rectangle of Stars Using Nested Loops

The document provides C++ code examples for printing patterns using nested loops. It includes a rectangle of stars, a triangle of numbers, and a multiplication table. Each example demonstrates the use of for, while, and do-while loops to manage rows and columns.

Uploaded by

taimoorsandhu841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Printing rectangle of stars

output
//*****
//*****
//*****
//outer loop always manage rows
//inner loop always manage columns
#include<iostream>
using namespace std;
int main()
{
int rows = 3;
int cols = 5;
for (int i = 1; i <= rows; i++)//outer loop
{
for (int j = 1; j <= cols; j++)//inner loop
{
cout << "*";
}
cout << endl;
}
return 0;
}
//same program in while loop
//*****
//*****
//*****
//outer loop always manage rows
//inner loop always manage columns
#include<iostream>
using namespace std;
int main()
{
int rows = 3;
int cols = 5;
int i = 1;
while (i <= rows) // i==1,1<=3 true,now i==2,2<=3 true,now i==3,3==3 true,now
i==4 4<=3 false
{
int j = 1;//i==2 remain same during this iteration,j==1
while (j <= cols)//1<=5 true,2<=5 true,3<=5 true,4<=5 true,5==5 true,6<=5 false
{
cout << "*"; //on screen printing *****
// *****
// *****
j++; //j==2,j==3,j==4,j==5,j==6
}
cout << endl;//move to next line
i++; //i==2,now i==3,now i==4
}
return 0;
}
//same program in do while loop
//*****
//*****
//*****
//outer loop always manage rows
//inner loop always manage columns
#include<iostream>
using namespace std;
int main()
{
int rows = 3;
int cols = 5;
int i = 1;
do {
int j = 1;//i==1 remain same during this iteration,j==1
do {
cout << "*"; //on screen printing *****
// *****
// *****
j++; //j==2,j==3,j==4,j==5,j==6

} while (j <= cols);//2<=5 true,3<=5 true,4<=5 true,5==5 true,6<=5 false


cout << endl;
i++;//i==2,now i==3,now i==4

} while (i <= rows); // now i==2,2<=3 true,now i==3,3==3 true,now i==4 4<=3 false
return 0;
}
Triangle of Numbers
Output
//1
//1 2
//1 2 3
//1 2 3 4
//1 2 3 4 5
#include<iostream>
using namespace std;
int main()
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}
Same no of row equal to same number of column
//1 2 3 4 5 i=1 j=1 to 5
//2 4 6 8 10 i=2,j=1 to 5
//3 6 9 12 15 i=3,j=1 to 5
//4 8 12 16 20 i=4,j=1 to 5
//5 10 15 20 25 i=5,j=1 to 5
#include<iostream>
using namespace std;
int main()
{
//int rows = 5;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
cout <<i*j << "\t";
}
cout << endl;
}
return 0;
}

You might also like