SlideShare a Scribd company logo
1 of 16
Nested Loops
• What is a nested loop?
• Types of nested loops
• Working of a nested loop
• Example Programs
What are nested loops?
• A nested loop is a loop
inside the body of
another loop.
• The nested loop is
known as the inner loop
and the loop in which it
is nested is known as the
outer loop
for ( int i=0; i< 3; i++)
{
for (int j = 0; j<3; j++)
cout<< j << “ “;
}
Outer loop
Inner loop
Outerloopbody
Innerloopbody
Types of Nested Loops
Nested for Nested while Nested do while Mixed
Nested for loop
for ( initialization; test; update) outer loop
{ //outer loop statements;
for( initialization; test; update) inner loop
{
//inner loop statements;
}
}
Nested while loop
while ( test) outer loop
{ //outer loop statements;
while( test) inner loop
{
//inner loop statements;
}
}
Nested do …. while loop
do outer loop
{ //outer loop statements;
do inner loop
{
//inner loop statements;
} while (test);
} while(test);
Working of nested loops : example 1
For each iteration of the outer loop, the inner loop is completely executed.
So for example for the following nested loop:
for(int i=0; i<3;i++)
for(int j=0; j<3;j++)
cout<<j<<“ ” ;
the statement, cout<<j<<“ ” ,will execute 3X3=9 times. For each iteration of
outer loop( i = 0, 1, 2) , inner loop will execute 3 times(j= 0, 1, 2).
Also note that there are no brackets. This is because both loops contain
single statements; outer loop contains inner loop and inner loop contains
single cout statement.
Working of nested loops : example 2
If the loop contains multiple statements then there is a need for brackets.
For the following nested loops:
for(int i=0; i<2;i++)
{ cout<<“n”;
for(int j=0; j<3;j++)
cout<<j<<“ ” ;
}
the statement, cout<<j<<“ ” ,will execute 2X3=6 times. For each iteration of outer
loop( i = 0, 1) , inner loop will execute 3 times(j= 0, 1, 2).
There are brackets in the above nested loop. This is because the outer loop
contains two statements; one cout<<“n” and the inner loop . The inner loop
contains single cout statement thus there are no brackets for inner loop.
Working of nested loops : example 3
If the loop contains multiple statements then there is a need for brackets.
For the following nested loops:
for(int i=0; i< 4;i++)
{ cout<<“n”;
for(int j=1; j<= 2;j++)
{cout<<j<<“ ” ;
cout<<“inner loop”;
}}
There are brackets in both the above nested loop. This is because the outer loop contains
two statements; one cout<<“n” and the inner loop . The inner loop contains two
statements 1.cout<<j<<“”and cout <<“inner loop “.
For each iteration of outer loop( i = 0, 1,2,3) , inner loop will execute 2 times(j= 1, 2). So
total no of times= 4X2=8
Working of Nested Loops
for(int i= 0 ; i< 2; i++)
{
cout<<“n”;
for(int j = 0; j<2; j ++)
cout<< j << “ ”;
}
1 2
3
4 5
6
8
7
1. STEP 1:The outer loop is initialized with value of i as 0
2. STEP 2: Value of i is tested, since the condition is true( i<2), the loop
is entered
3. STEP 3:A newline is displayed (cout<<“n”;). This is part of outer loop.
4. STEP 4 :The control goes to inner loop, where j is initialized with 0
5. STEP 5: Value of j is tested, j<2 is true, inner loop is entered
6. STEP 6: The statement cout << j << “”is executed, value of j is displayed
7. STEP 7: The value of j is incremented.
8. Now STEPs 5, 6 and 7 are repeated till the condition ,j<2,
becomes false.
9. When value of j is 2, control comes out of inner loop.
8. STEP 8: Now the control goes to outer loop update statement,
i is incremented.
9. STEPs 2 – 7 are repeated. The steps are repeated for value of i = 1
This continues till value of i becomes 2. Then the outer loop is
terminated.
Working of nested loops
Output:
0 1
0 1
for(int i= 0 ; i< 2; i++)
{
cout<<“n”;
for(int j = 0; j<2; j ++)
cout<< j << “ ”;
}
0
i
1
0
1
j
0
1
i
1 2
3
6
4 5 7
8
2
2 2
i
Example Programs
Program: To print the pattern
#include<iostream.h>
void main()
{
for( int x = 1; x < 3; x ++)
{
for ( int y = 1; y <= x; y ++)
cout<<y <<”t”;
cout<< ”n” ;
}}
1
1 2
1 2 3
Explanation
1.Outer loop : 1st Iteration x=1
1.1 Inner Loop : 1st Iteration,y=1 cout<<y<<“t
Increment y , y=2, check y<=x=false Inner loop over
2. Outer loop :2nd Iteration x=2
2.1 Inner loop : 1st Iteration ,y=1 cout<< y<<“t”
2.2 Inner loop : 2nd Iteration, y=2 cout<<y<<“t”
Increment y , y=3, check y<=x=false Inner loop over
3. Outer loop :3rd Iteration x=3
3.1 Inner loop : 1st Iteration y=1, cout<<y<<“t”
3.2 Inner loop : 2nd Iteration, y=2, cout<<y<<“t”
3.3 Inner loop : 3rd Iteration , y = 3, cout<<y<<“t”
Increment y , y=4, check y<=x=false Inner loop over
increment x, x= 3, outer loop over
Output
Program :Program to find the divisors of numbers
entered.
#include<iostream.h>
#include<conio.h>
void main()
{ int n, k;
cout<< “Enter the number of integers :”;
cin >> n;
for( int i = 0; i < n; i ++)
{ cout<< “Enter the number of whose divisor are to be found”;
cin >> k;
cout << “n The divisor are :”<<”n”;
for ( int j = 1; j <= k/2; j ++)
if( k%j == 0)
cout<< j <<”t”;
}
EXPLANATION:
The outer loop keeps track of the
number of integer a user inputs.
The inner loop calculates and displays
its divisors
Enter the number of integers : 2
Enter the number whose divisor is to be found : 6
The divisors are :
1 2 3
Enter the number whose divisor is to be found : 15
The divisors are :
1 3 5
Output
Program : Display the multiplication table of a number till the
user wishes
#include<iostream.h>
#include<conio.h>
void main()
{
int num, l;
char ch;
do
{cout<<”Enter a number whose multiplication table is to be displayed:
”;
cin>>num;
cout<<”Enter the limit of upto which table is to be displayed :”;
cin>>l;
int i = 1;
while( i <= l){
{
cout<<num<<”X”<< i <<”=”<<num*i <<”n”;}
cout<<”Do you wish to continue (Y/N)”<<”n”;
cin>>ch; } while(ch!=’n’||ch!=’N’);}}
Enter a number whose multiplication table is to
be displayed: 8
Enter the limit upto which table is to be
displayed : 4
8X1=8
8X2=16
8X3=24
8X4=32
Do you wish to continue(Y/N)
Y
Enter a number whose multiplication table is to
be displayed: 6
Enter the limit upto which table is to be
displayed : 5
6X1=6
6X2=12
6X3=18
6X4=24
6X5=30
Do you wish to continue(Y/N)
N
Output
Nested loops

More Related Content

What's hot

Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

What's hot (20)

Method overloading
Method overloadingMethod overloading
Method overloading
 
Nested loops
Nested loopsNested loops
Nested loops
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Loops in c
Loops in cLoops in c
Loops in c
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Structure in C
Structure in CStructure in C
Structure in C
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Array in c
Array in cArray in c
Array in c
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 

Similar to Nested loops

FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
AliaaAqilah3
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 

Similar to Nested loops (20)

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
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
Iteration
IterationIteration
Iteration
 
06.Loops
06.Loops06.Loops
06.Loops
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Ch4
Ch4Ch4
Ch4
 
C++ loop
C++ loop C++ loop
C++ loop
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptx
 

More from Neeru Mittal

More from Neeru Mittal (16)

Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Introduction to AI and its domains.pptx
Introduction to AI and its domains.pptxIntroduction to AI and its domains.pptx
Introduction to AI and its domains.pptx
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in Python
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and Tricks
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV Connectivity
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 

Nested loops

  • 1. Nested Loops • What is a nested loop? • Types of nested loops • Working of a nested loop • Example Programs
  • 2. What are nested loops? • A nested loop is a loop inside the body of another loop. • The nested loop is known as the inner loop and the loop in which it is nested is known as the outer loop for ( int i=0; i< 3; i++) { for (int j = 0; j<3; j++) cout<< j << “ “; } Outer loop Inner loop Outerloopbody Innerloopbody
  • 3. Types of Nested Loops Nested for Nested while Nested do while Mixed
  • 4. Nested for loop for ( initialization; test; update) outer loop { //outer loop statements; for( initialization; test; update) inner loop { //inner loop statements; } }
  • 5. Nested while loop while ( test) outer loop { //outer loop statements; while( test) inner loop { //inner loop statements; } }
  • 6. Nested do …. while loop do outer loop { //outer loop statements; do inner loop { //inner loop statements; } while (test); } while(test);
  • 7. Working of nested loops : example 1 For each iteration of the outer loop, the inner loop is completely executed. So for example for the following nested loop: for(int i=0; i<3;i++) for(int j=0; j<3;j++) cout<<j<<“ ” ; the statement, cout<<j<<“ ” ,will execute 3X3=9 times. For each iteration of outer loop( i = 0, 1, 2) , inner loop will execute 3 times(j= 0, 1, 2). Also note that there are no brackets. This is because both loops contain single statements; outer loop contains inner loop and inner loop contains single cout statement.
  • 8. Working of nested loops : example 2 If the loop contains multiple statements then there is a need for brackets. For the following nested loops: for(int i=0; i<2;i++) { cout<<“n”; for(int j=0; j<3;j++) cout<<j<<“ ” ; } the statement, cout<<j<<“ ” ,will execute 2X3=6 times. For each iteration of outer loop( i = 0, 1) , inner loop will execute 3 times(j= 0, 1, 2). There are brackets in the above nested loop. This is because the outer loop contains two statements; one cout<<“n” and the inner loop . The inner loop contains single cout statement thus there are no brackets for inner loop.
  • 9. Working of nested loops : example 3 If the loop contains multiple statements then there is a need for brackets. For the following nested loops: for(int i=0; i< 4;i++) { cout<<“n”; for(int j=1; j<= 2;j++) {cout<<j<<“ ” ; cout<<“inner loop”; }} There are brackets in both the above nested loop. This is because the outer loop contains two statements; one cout<<“n” and the inner loop . The inner loop contains two statements 1.cout<<j<<“”and cout <<“inner loop “. For each iteration of outer loop( i = 0, 1,2,3) , inner loop will execute 2 times(j= 1, 2). So total no of times= 4X2=8
  • 10. Working of Nested Loops for(int i= 0 ; i< 2; i++) { cout<<“n”; for(int j = 0; j<2; j ++) cout<< j << “ ”; } 1 2 3 4 5 6 8 7 1. STEP 1:The outer loop is initialized with value of i as 0 2. STEP 2: Value of i is tested, since the condition is true( i<2), the loop is entered 3. STEP 3:A newline is displayed (cout<<“n”;). This is part of outer loop. 4. STEP 4 :The control goes to inner loop, where j is initialized with 0 5. STEP 5: Value of j is tested, j<2 is true, inner loop is entered 6. STEP 6: The statement cout << j << “”is executed, value of j is displayed 7. STEP 7: The value of j is incremented. 8. Now STEPs 5, 6 and 7 are repeated till the condition ,j<2, becomes false. 9. When value of j is 2, control comes out of inner loop. 8. STEP 8: Now the control goes to outer loop update statement, i is incremented. 9. STEPs 2 – 7 are repeated. The steps are repeated for value of i = 1 This continues till value of i becomes 2. Then the outer loop is terminated.
  • 11. Working of nested loops Output: 0 1 0 1 for(int i= 0 ; i< 2; i++) { cout<<“n”; for(int j = 0; j<2; j ++) cout<< j << “ ”; } 0 i 1 0 1 j 0 1 i 1 2 3 6 4 5 7 8 2 2 2 i
  • 13. Program: To print the pattern #include<iostream.h> void main() { for( int x = 1; x < 3; x ++) { for ( int y = 1; y <= x; y ++) cout<<y <<”t”; cout<< ”n” ; }} 1 1 2 1 2 3 Explanation 1.Outer loop : 1st Iteration x=1 1.1 Inner Loop : 1st Iteration,y=1 cout<<y<<“t Increment y , y=2, check y<=x=false Inner loop over 2. Outer loop :2nd Iteration x=2 2.1 Inner loop : 1st Iteration ,y=1 cout<< y<<“t” 2.2 Inner loop : 2nd Iteration, y=2 cout<<y<<“t” Increment y , y=3, check y<=x=false Inner loop over 3. Outer loop :3rd Iteration x=3 3.1 Inner loop : 1st Iteration y=1, cout<<y<<“t” 3.2 Inner loop : 2nd Iteration, y=2, cout<<y<<“t” 3.3 Inner loop : 3rd Iteration , y = 3, cout<<y<<“t” Increment y , y=4, check y<=x=false Inner loop over increment x, x= 3, outer loop over Output
  • 14. Program :Program to find the divisors of numbers entered. #include<iostream.h> #include<conio.h> void main() { int n, k; cout<< “Enter the number of integers :”; cin >> n; for( int i = 0; i < n; i ++) { cout<< “Enter the number of whose divisor are to be found”; cin >> k; cout << “n The divisor are :”<<”n”; for ( int j = 1; j <= k/2; j ++) if( k%j == 0) cout<< j <<”t”; } EXPLANATION: The outer loop keeps track of the number of integer a user inputs. The inner loop calculates and displays its divisors Enter the number of integers : 2 Enter the number whose divisor is to be found : 6 The divisors are : 1 2 3 Enter the number whose divisor is to be found : 15 The divisors are : 1 3 5 Output
  • 15. Program : Display the multiplication table of a number till the user wishes #include<iostream.h> #include<conio.h> void main() { int num, l; char ch; do {cout<<”Enter a number whose multiplication table is to be displayed: ”; cin>>num; cout<<”Enter the limit of upto which table is to be displayed :”; cin>>l; int i = 1; while( i <= l){ { cout<<num<<”X”<< i <<”=”<<num*i <<”n”;} cout<<”Do you wish to continue (Y/N)”<<”n”; cin>>ch; } while(ch!=’n’||ch!=’N’);}} Enter a number whose multiplication table is to be displayed: 8 Enter the limit upto which table is to be displayed : 4 8X1=8 8X2=16 8X3=24 8X4=32 Do you wish to continue(Y/N) Y Enter a number whose multiplication table is to be displayed: 6 Enter the limit upto which table is to be displayed : 5 6X1=6 6X2=12 6X3=18 6X4=24 6X5=30 Do you wish to continue(Y/N) N Output