SlideShare a Scribd company logo
PATTERN
PRINTING USING
C++
PROGRAMMING
(NESTING OF FOR
LOOPS)
CREDITS: WE THE CODING GUYS
Page 1 of 10
Pattern 1
Explanation of the logic
In above figure you see certain number of
rows and columns. You can see in 1st
row we
have 1 star, in 2nd
row we have 2 stars. So in
every row, we have row number of stars.
In pattern printing always parent loop is for
rows and inner loop is for columns.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=5;row++)
{
for(col=1;col<=row;col++) /*printing no of
stars for each row*/
{
cout<<'*';
}
cout<<endl;
}
return 0;
}
Pattern 2
Explanation of the logic
Page 2 of 10
In first row we have 5 stars, in second row we
have 4 stars and so on. So no of stars are
decreasing by 1 as you go to the next row.
Logic is same as of previous pattern, only
difference is that we have to decrement the
stars this time.
Program
#include <iostream>
using namespace std;
int main(){
int row,col;
for(row=1;row<=5;row++){
for(col=5;col>=row;col--) //printing no of stars
for each row
{
cout<<'*';
}
cout<<endl;
}
return 0;
}
Pattern 3
Explanation of the logic
In above figure, the symbol ‘-’ represents
spaces. In first row, we need to print 4 spaces
and then 1 star. In second row, we need to
print 3 spaces and 2 stars. So we see that no
of spaces are decreasing and no of stars are
increasing.
If we divide above figure in two parts then we
will have
Part 1: Part 2:
We have already written codes for these two
patterns previously. So we just need to
combine these two to get the results.
Page 3 of 10
Program
#include <iostream>
using namespace std;
int main(){
int row,col,col2;
for(row=1;row<=5;row++){
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=row;col2++) //printing no of
stars for each row
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Pattern 4
Explanation of the logic
So first analyze the above figure. You see that
logic is exactly same as above program,
difference is that, when you will print the
stars, then provide a space after that i.e.
instead of printing “*” , you should write “*-”
in cout command. The ‘-’ symbol above
represents space here for ease of
understanding.
Program
#include <iostream>
using namespace std;
int main(){
int row,col,col2;
for(row=1;row<=5;row++){
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=row;col2++)//printing no of
stars for each row
{
cout<<"* "; //provide space after star by
hitting space bar of your keyboard
Page 4 of 10
}
cout<<endl;
}
return 0;
}
Pattern 5
Explanation of the logic
Here the no of stars to be printed follow the
fomula: (ROW*2)-1.
For example, consider the no of stars to be
printed in 4th
row.
(4*2)-1 =7.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
for(row=1;row<=5;row++)
{
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=(row*2)-1;col2++)//printing
no of stars for each row
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Page 5 of 10
Pattern 6
Explanation of the logic:
This logic is same as previous logic of pattern
6. What we need is to add its reverse part
below it.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
for(row=1;row<=5;row++){
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=(row*2)-1;col2++)//printing
no of stars for each row
{
cout<<"*";
}
cout<<endl;
}
for(row=1;row<=4;row++){
for(col=1;col<=row;col++)
{
cout<<" ";
}
for(col2=7;col2>=(row*2)-1;col2--)
{
cout<<"*";
}
cout<<endl;
}
return 0;
Page 6 of 10
}
Pattern 7
Explanation of the logic
Now analyze this pattern. See the previous
pattern and try to analyze that stars are
replaced by spaces and spaces by stars. So if
you do that, you will get following pattern:
i.e. we will get half the required code on left
side. So we just need to copy paste that code
of star printing to right side also to get the full
pattern. It will be clear when you will run the
program.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
for(row=1;row<=5;row++)
{
for(col=4;col>=row;col--) //printing no of stars
for each row
{
cout<<"*";
}
for(col2=1;col2<=(row*2)-1;col2++)//printing
no of spaces for each row
{
cout<<" ";
}
for(col=4;col>=row;col--) //printing no of stars
for each row
Page 7 of 10
{
cout<<"*";
}
cout<<endl;
}
for(row=1;row<=4;row++)
{
for(col=1;col<=row;col++) //printing no of
stars for each row {
cout<<"*";
}
for(col2=7;col2>=(row*2)-1;col2--) //printing
no of spaces each row {
cout<<" ";
}
for(col=1;col<=row;col++) //printing no of
stars each row
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Pattern 8
Explanation of the logic
The logic is same as that of Pattern 4,
difference is that here we are printing
alternate ‘#’ and ‘*’.
So to do that we are going to create a variable
named “count=1”, now if this count will be
odd then star will be printed and if this count
will be even then hash will be printed. This
will be more clear when you see the following
code.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
int count=1;
for(row=1;row<=5;row++)
{
Page 8 of 10
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=row;col2++)
{
if(count%2==0) //printing alternate star and
hash
cout<<"# ";
else
cout<<"* ";
count++;
}
cout<<endl;
}
return 0;
}
Pattern 9
Explanation of the logic
This is Pascal’s triangle, which can be printed
using combinations as seen above. This will be
more easy if we print this pattern using
recursion.
Program
#include <iostream>
using namespace std;
int fact(int n)
{
if(n==0|n==1)
return 1;
else
return n*fact(n-1);
}
int nCr(int n, int r)
{
return fact(n)/(fact(r)*fact(n-r));
}
int main()
{
int row,col,col1;
for(row=0;row<5;row++)
{
Page 9 of 10
for(col=5-row;col>0;col--){
cout<<" ";
}
for(col1=0;col1<=row;col1++){
cout<<" "<<nCr(row,col1);
}
cout<<endl;
}
return 0;
}
Pattern 10:
Explanation of the logic
This is similar to pattern 1 printing; we just
need to do some minor changes in program.
So here we have to print column%2. This will
be more clear when you will see the following
code.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col;
;
for(row=1;row<=5;row++)
{
for(col=1;col<=row;col++)
{
int remainder=col%2;
cout<<remainder;
}
cout<<endl;
}
return 0;
}
Page 10 of 10
Pattern 11
Explanation of the logic
Here we need to print column*row.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col;
;
for(row=1;row<=5;row++)
{
for(col=1;col<=row;col++)
{
int print=col*row;
cout<<print;
}
cout<<endl;
}
return 0;
}

More Related Content

What's hot

Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
Aman Deep
 
C program language tutorial pattern printing
C program language tutorial pattern printingC program language tutorial pattern printing
C program language tutorial pattern printing
Sourav Ganguly
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
Svetlin Nakov
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
Syed Mustafa
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Strings
StringsStrings
Strings
Nilesh Dalvi
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
Jawad Khan
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
Sql injection
Sql injectionSql injection
Sql injection
Mehul Boghra
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
String functions in C
String functions in CString functions in C
Loops in Python
Loops in PythonLoops in Python
Loops in Python
Arockia Abins
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
Aditya Vaishampayan
 
Structure & union
Structure & unionStructure & union
Structure & union
Rupesh Mishra
 

What's hot (20)

Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
C program language tutorial pattern printing
C program language tutorial pattern printingC program language tutorial pattern printing
C program language tutorial pattern printing
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Strings
StringsStrings
Strings
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Sql injection
Sql injectionSql injection
Sql injection
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
String functions in C
String functions in CString functions in C
String functions in C
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
Structure & union
Structure & unionStructure & union
Structure & union
 

Viewers also liked

C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
The Loops
The LoopsThe Loops
The Loops
Krishma Parekh
 
Loops
LoopsLoops
Loops in C
Loops in CLoops in C
Loops in C
Kamal Acharya
 
C++ loop
C++ loop C++ loop
C++ loop
Khelan Ameen
 
Loops
LoopsLoops
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
Himanshu Negi
 
For...next loop structure
For...next loop structureFor...next loop structure
For...next loop structure
Jd Mercado
 
Loops
LoopsLoops
Loops
LoopsLoops
Loops Basics
Loops BasicsLoops Basics
Loops Basics
Mushiii
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 

Viewers also liked (13)

C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
The Loops
The LoopsThe Loops
The Loops
 
Loops
LoopsLoops
Loops
 
Loops in C
Loops in CLoops in C
Loops in C
 
C++ loop
C++ loop C++ loop
C++ loop
 
Loops
LoopsLoops
Loops
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
For...next loop structure
For...next loop structureFor...next loop structure
For...next loop structure
 
Loops
LoopsLoops
Loops
 
Loops
LoopsLoops
Loops
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 

Similar to Nesting of for loops using C++

-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
Adamq0DJonese
 
week4_python.docx
week4_python.docxweek4_python.docx
week4_python.docx
Radhe Syam
 
a) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfa) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdf
anuradhasilks
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
Aman Deep
 
Lecture1 classes1
Lecture1 classes1Lecture1 classes1
Lecture1 classes1
Noor Faezah Mohd Yatim
 
C++ programming pattern
C++ programming patternC++ programming pattern
C++ programming pattern
Raushankumar550
 
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon2017_Cpp  a monster that no one likes but that will outlast them all _Ya...GECon2017_Cpp  a monster that no one likes but that will outlast them all _Ya...
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon_Org Team
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allGECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
Yauheni Akhotnikau
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
Mahmoud Samir Fayed
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
Dr. Md. Shohel Sayeed
 
C++ programming
C++ programmingC++ programming
C++ programming
Abdallah Abuouf
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 
Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++
sujathavvv
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
AAKASH KUMAR
 
Trees And More With Postgre S Q L
Trees And  More With  Postgre S Q LTrees And  More With  Postgre S Q L
Trees And More With Postgre S Q L
PerconaPerformance
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
NUST Stuff
 
Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3
hasi071
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
RithikRaj25
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
Marco Izzotti
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
amitbagga0808
 

Similar to Nesting of for loops using C++ (20)

-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
 
week4_python.docx
week4_python.docxweek4_python.docx
week4_python.docx
 
a) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfa) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdf
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
Lecture1 classes1
Lecture1 classes1Lecture1 classes1
Lecture1 classes1
 
C++ programming pattern
C++ programming patternC++ programming pattern
C++ programming pattern
 
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon2017_Cpp  a monster that no one likes but that will outlast them all _Ya...GECon2017_Cpp  a monster that no one likes but that will outlast them all _Ya...
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allGECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
C++ programming
C++ programmingC++ programming
C++ programming
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
Trees And More With Postgre S Q L
Trees And  More With  Postgre S Q LTrees And  More With  Postgre S Q L
Trees And More With Postgre S Q L
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 

Recently uploaded

Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 

Recently uploaded (20)

Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 

Nesting of for loops using C++

  • 1. PATTERN PRINTING USING C++ PROGRAMMING (NESTING OF FOR LOOPS) CREDITS: WE THE CODING GUYS
  • 2. Page 1 of 10 Pattern 1 Explanation of the logic In above figure you see certain number of rows and columns. You can see in 1st row we have 1 star, in 2nd row we have 2 stars. So in every row, we have row number of stars. In pattern printing always parent loop is for rows and inner loop is for columns. Program #include <iostream> using namespace std; int main() { int row,col; for(row=1;row<=5;row++) { for(col=1;col<=row;col++) /*printing no of stars for each row*/ { cout<<'*'; } cout<<endl; } return 0; } Pattern 2 Explanation of the logic
  • 3. Page 2 of 10 In first row we have 5 stars, in second row we have 4 stars and so on. So no of stars are decreasing by 1 as you go to the next row. Logic is same as of previous pattern, only difference is that we have to decrement the stars this time. Program #include <iostream> using namespace std; int main(){ int row,col; for(row=1;row<=5;row++){ for(col=5;col>=row;col--) //printing no of stars for each row { cout<<'*'; } cout<<endl; } return 0; } Pattern 3 Explanation of the logic In above figure, the symbol ‘-’ represents spaces. In first row, we need to print 4 spaces and then 1 star. In second row, we need to print 3 spaces and 2 stars. So we see that no of spaces are decreasing and no of stars are increasing. If we divide above figure in two parts then we will have Part 1: Part 2: We have already written codes for these two patterns previously. So we just need to combine these two to get the results.
  • 4. Page 3 of 10 Program #include <iostream> using namespace std; int main(){ int row,col,col2; for(row=1;row<=5;row++){ for(col=4;col>=row;col--) //printing no of spaces for each row { cout<<" "; } for(col2=1;col2<=row;col2++) //printing no of stars for each row { cout<<"*"; } cout<<endl; } return 0; } Pattern 4 Explanation of the logic So first analyze the above figure. You see that logic is exactly same as above program, difference is that, when you will print the stars, then provide a space after that i.e. instead of printing “*” , you should write “*-” in cout command. The ‘-’ symbol above represents space here for ease of understanding. Program #include <iostream> using namespace std; int main(){ int row,col,col2; for(row=1;row<=5;row++){ for(col=4;col>=row;col--) //printing no of spaces for each row { cout<<" "; } for(col2=1;col2<=row;col2++)//printing no of stars for each row { cout<<"* "; //provide space after star by hitting space bar of your keyboard
  • 5. Page 4 of 10 } cout<<endl; } return 0; } Pattern 5 Explanation of the logic Here the no of stars to be printed follow the fomula: (ROW*2)-1. For example, consider the no of stars to be printed in 4th row. (4*2)-1 =7. Program #include <iostream> using namespace std; int main() { int row,col,col2; for(row=1;row<=5;row++) { for(col=4;col>=row;col--) //printing no of spaces for each row { cout<<" "; } for(col2=1;col2<=(row*2)-1;col2++)//printing no of stars for each row { cout<<"*"; } cout<<endl; } return 0; }
  • 6. Page 5 of 10 Pattern 6 Explanation of the logic: This logic is same as previous logic of pattern 6. What we need is to add its reverse part below it. Program #include <iostream> using namespace std; int main() { int row,col,col2; for(row=1;row<=5;row++){ for(col=4;col>=row;col--) //printing no of spaces for each row { cout<<" "; } for(col2=1;col2<=(row*2)-1;col2++)//printing no of stars for each row { cout<<"*"; } cout<<endl; } for(row=1;row<=4;row++){ for(col=1;col<=row;col++) { cout<<" "; } for(col2=7;col2>=(row*2)-1;col2--) { cout<<"*"; } cout<<endl; } return 0;
  • 7. Page 6 of 10 } Pattern 7 Explanation of the logic Now analyze this pattern. See the previous pattern and try to analyze that stars are replaced by spaces and spaces by stars. So if you do that, you will get following pattern: i.e. we will get half the required code on left side. So we just need to copy paste that code of star printing to right side also to get the full pattern. It will be clear when you will run the program. Program #include <iostream> using namespace std; int main() { int row,col,col2; for(row=1;row<=5;row++) { for(col=4;col>=row;col--) //printing no of stars for each row { cout<<"*"; } for(col2=1;col2<=(row*2)-1;col2++)//printing no of spaces for each row { cout<<" "; } for(col=4;col>=row;col--) //printing no of stars for each row
  • 8. Page 7 of 10 { cout<<"*"; } cout<<endl; } for(row=1;row<=4;row++) { for(col=1;col<=row;col++) //printing no of stars for each row { cout<<"*"; } for(col2=7;col2>=(row*2)-1;col2--) //printing no of spaces each row { cout<<" "; } for(col=1;col<=row;col++) //printing no of stars each row { cout<<"*"; } cout<<endl; } return 0; } Pattern 8 Explanation of the logic The logic is same as that of Pattern 4, difference is that here we are printing alternate ‘#’ and ‘*’. So to do that we are going to create a variable named “count=1”, now if this count will be odd then star will be printed and if this count will be even then hash will be printed. This will be more clear when you see the following code. Program #include <iostream> using namespace std; int main() { int row,col,col2; int count=1; for(row=1;row<=5;row++) {
  • 9. Page 8 of 10 for(col=4;col>=row;col--) //printing no of spaces for each row { cout<<" "; } for(col2=1;col2<=row;col2++) { if(count%2==0) //printing alternate star and hash cout<<"# "; else cout<<"* "; count++; } cout<<endl; } return 0; } Pattern 9 Explanation of the logic This is Pascal’s triangle, which can be printed using combinations as seen above. This will be more easy if we print this pattern using recursion. Program #include <iostream> using namespace std; int fact(int n) { if(n==0|n==1) return 1; else return n*fact(n-1); } int nCr(int n, int r) { return fact(n)/(fact(r)*fact(n-r)); } int main() { int row,col,col1; for(row=0;row<5;row++) {
  • 10. Page 9 of 10 for(col=5-row;col>0;col--){ cout<<" "; } for(col1=0;col1<=row;col1++){ cout<<" "<<nCr(row,col1); } cout<<endl; } return 0; } Pattern 10: Explanation of the logic This is similar to pattern 1 printing; we just need to do some minor changes in program. So here we have to print column%2. This will be more clear when you will see the following code. Program #include <iostream> using namespace std; int main() { int row,col; ; for(row=1;row<=5;row++) { for(col=1;col<=row;col++) { int remainder=col%2; cout<<remainder; } cout<<endl; } return 0; }
  • 11. Page 10 of 10 Pattern 11 Explanation of the logic Here we need to print column*row. Program #include <iostream> using namespace std; int main() { int row,col; ; for(row=1;row<=5;row++) { for(col=1;col<=row;col++) { int print=col*row; cout<<print; } cout<<endl; } return 0; }