0% found this document useful (0 votes)
35 views14 pages

C++ Nested Loop Patterns and Programs

The document outlines a programming lab task for students at the University of Sialkot, focusing on C++ programming. It includes several exercises that require students to write programs using nested loops to create various patterns such as pyramids, inverted pyramids, diamonds, number triangles, and Floyd's Triangle. Each task is accompanied by sample code and expected output.

Uploaded by

tanzilijaz3
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)
35 views14 pages

C++ Nested Loop Patterns and Programs

The document outlines a programming lab task for students at the University of Sialkot, focusing on C++ programming. It includes several exercises that require students to write programs using nested loops to create various patterns such as pyramids, inverted pyramids, diamonds, number triangles, and Floyd's Triangle. Each task is accompanied by sample code and expected output.

Uploaded by

tanzilijaz3
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

Department Of Artificial

Intelligence
University Of Sialkot

Programming
Fundamental Lab
Task#9

Syeda Arfa Gillani (012)


Zainab Imran (019)
Muhammad Tanzil Ijaz Marth (015)
Muhammad Bilal Imran Shahid (023)
BSAI Blue
Semester: 1st

Components:

Department Of Artificial Intelligence


University Of Sialkot
1:
Write a C++ program to print the following pyramid pattern using nested loops:
*
**
***
****

#include<iostream>
using namespace std;
int main (){
for (int i=1;i<=4;i++){
for (int j=4;j>=i;j--){
cout<<" ";
}
for (int k=1;k<=i;k++){
cout<<"* ";
}
cout<<endl;
}
}

Output:
2:
Write a C++ program to print the following inverted pyramid pattern using nested loops:
****
***
**
*
#include<iostream>
using namespace std;
int main (){
for (int i=1;i<=4;i++){
for (int j=1;j<=i;j++)
{
cout<<" ";
}
for (int k=4;k>=i;k--){
cout<<"* ";
}
cout<<endl;
}
}

Output:
3:
Write a C++ program to print the following diamond pattern using nested loops:
*
**
***
****
***
**
*
#include<iostream>
using namespace std;
int main (){
for (int i=1;i<=4;i++){
for (int j=3;j>=i;j--){
cout<<" ";
}
for (int k=1;k<=i;k++){
cout<<"* ";
}
cout<<endl;
}
for (int i=1;i<=3;i++){
for (int j=1;j<=i;j++){
cout<<" ";}
for (int k=3;k>=i;k--){
cout<<"* ";}
cout<<endl;
}
}
Output:

4:
Write a C++ program using nested loops to print the following number triangle for n =
4:
1
12
123
1234
#include<iostream>
using namespace std;
int main (){
for (int i=1;i<=4;i++){
for (int j=1;j<=i;j++){
cout<<j;
}
cout<<endl;
}
}

Output:
5:
Write a C++ program using nested loops to print the following reverse number triangle
for n=4:
1234
123
12
1

#include<iostream>
using namespace std;
int main(){
for(int i=4;i>=1;i--){
for(int j=1;j<=i;j++){
cout<<j;
}
cout<<endl;
}
}

Output:
6:
Write a C++ program to print Floyd’s Triangle for n=4, using nested loops:
1
23
456
7 8 9 10

#include<iostream>
using namespace std;
int main(){
int num=1;
for (int i=1;i<=4;i++){
for (int j=1;j<=i;j++){
cout<<num<<" ";
num++;
}
cout<<endl;
}
}

Output:
7:
Write a C++ program to print the following pattern using nested loops:

(i)

#include<iostream>
using namespace std;
int main(){
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
}
Output:

(ii)

#include<iostream>
using namespace std;
int main(){
for(int i=1;i<=5;i++){
for(int j=4;j>=i;j--){
cout<<" ";
}
for(int k=1;k<=i;k++){
cout<<"*";
}
cout<<endl;
}

Output:
(iii)

#include <iostream>
using namespace std;
int main ( ){
for (int i = 1; i <= 5; i++){
for (int j = 5; j >= r; j--) {
cout << "*";
}
cout << endl;
}
}

Output:
(iv)

#include <iostream>
using namespace std;
int main(){
for (int i = 1;i <= 5; i++) {
for (int j=1;j<= i; j++){
cout << " ";
}
for (int k = 5; k >= i; k--){
cout << "*";
}
cout << endl;
}
}

Output:
(v)

#include <iostream>
using namespace std;
int main (){
for (int i= 1;i <= 5;i++){
for (int j = 1; j <= 5; j++) {
cout << "*";
}
cout << endl;
}
}

Output:
(vi)

#include <iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
if (i == 1 || i == 5 || j == 1 || j == 5)
{
cout << "* ";
}
else
{
cout << " ";
}
}
cout << endl;
}
}

Output:

You might also like