SlideShare a Scribd company logo
1 of 11
Download to read offline
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

Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++Jaspal Singh
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
 
Python Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxPython Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxShreyasLawand
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operatorsraksharao
 
Python data type
Python data typePython data type
Python data typenuripatidar
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm KristinaBorooah
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statementnarmadhakin
 
String in c programming
String in c programmingString in c programming
String in c programmingDevan Thakur
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structureSoham Nanekar
 

What's hot (20)

Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Python Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxPython Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptx
 
C string
C stringC string
C string
 
Nested loops
Nested loopsNested loops
Nested loops
 
Queues
QueuesQueues
Queues
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
Python data type
Python data typePython data type
Python data type
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
C++
C++C++
C++
 
Project on stack Data structure
Project on stack Data structureProject on stack Data structure
Project on stack Data structure
 

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.docxAdamq0DJonese
 
week4_python.docx
week4_python.docxweek4_python.docx
week4_python.docxRadhe 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.pdfanuradhasilks
 
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
 
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
 
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 allYauheni 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 202Mahmoud Samir Fayed
 
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 CSAAKASH 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 LPerconaPerformance
 
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 3hasi071
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++Marco Izzotti
 

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

Recently uploaded

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
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
 
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 Performancesivaprakash250
 

Recently uploaded (20)

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
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...
 
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
 

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