SlideShare una empresa de Scribd logo
1 de 11
Descargar para leer sin conexión
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;
}

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

String in c programming
String in c programmingString in c programming
String in c programming
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Data Types In C
Data Types In CData Types In C
Data Types In C
 
2D Array
2D Array 2D Array
2D Array
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Typedef
TypedefTypedef
Typedef
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
String c
String cString c
String c
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 

Destacado

For...next loop structure
For...next loop structureFor...next loop structure
For...next loop structure
Jd Mercado
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 

Destacado (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 a 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
 
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
 
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
 

Similar a 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)
 
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++
 

Último

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

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; }