SlideShare a Scribd company logo
Structured Programming Language
Nested Loop
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Nested Loop
• A nested loop is a loop within a loop i.e.
an inner loop within the body of an outer
one.
• The first pass of the outer loop triggers
the inner loop, which executes to
completion.
• Then the second pass of the outer loop
triggers the inner loop again.
• This repeats until the outer loop finishes.
Nested for Loop
#include <stdio.h>
int main()
{
int n=3, m=5;
int cnt=0,i,j;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
++cnt;
}
}
printf("inner loop iterates in total %d timesn",cnt);
return 0;
}
Trace
it……
Total no of Iterations
• When i=1, the inner loop executes m times
• When i=2, the inner loop executes m times
• .
• .
• .
• When i=n, the inner loop executes m times.
So total no of iterations = m + m + m+ … … … +m = n *m
Nested for Loop
Trace
it……
#include <stdio.h>
int main()
{
int n=10;
int cnt=0,i,j;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
++cnt;
}
}
printf("inner loop iterates in total %d timesn",cnt);
return 0;
}
Total no of Iterations
• When i=1, the inner loop executes 1 times
• When i=2, the inner loop executes 2 times
• When i=3, the inner loop executes 3 times
• .
• .
• When i=n, the inner loop executes n times.
So total no of iterations = 1 + 2 + 3+ … … … +n
= n(n+1) / 2
Flow Chart of Nested Loop
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* *
* *
* *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* *
* *
* *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
*
* *
* * *
* * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
*
* *
* * *
* * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
*
* *
* *
* *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * *
* * *
* *
*
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * *
* * *
* *
*
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Problem
• Input: n (integer)
• Output: the following shape when n=5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Problem
• Input: n (integer)
• Output: the following shape when n=5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Problem
• Input: n (integer)
• Output: the following shape when n=5
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Problem
• Input: n (integer)
• Output: the following shape when n=4
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
References
• https://www.programtopia.net/cprogramming/docs/
nested-loop
• Number pyramids:
http://www.codeforwin.in/2016/06/number-
pattern-programs-in-c.html
• Star pyramids:
http://www.codeforwin.in/2015/07/star-patterns-
program-in-c.html

More Related Content

What's hot

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
C++ ppt
C++ pptC++ ppt
C++ ppt
parpan34
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Himanshu Kaushik
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
Vandana Salve
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Python: Manager Objects
Python: Manager ObjectsPython: Manager Objects
Python: Manager Objects
Damian T. Gordon
 
Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
Mobeen Mustafa
 
Software Project Management( lecture 1)
Software Project Management( lecture 1)Software Project Management( lecture 1)
Software Project Management( lecture 1)
Syed Muhammad Hammad
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Stavros Vassos
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
MD Sulaiman
 
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERSVTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
vtunotesbysree
 
C fundamental
C fundamentalC fundamental
C fundamental
Selvam Edwin
 
Shell and its types in LINUX
Shell and its types in LINUXShell and its types in LINUX
Shell and its types in LINUX
SHUBHA CHATURVEDI
 
Design Concept software engineering
Design Concept software engineeringDesign Concept software engineering
Design Concept software engineering
Darshit Metaliya
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
ABhay Panchal
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
Bala Ganesh
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
 
Lecture 3 RE NFA DFA
Lecture 3   RE NFA DFA Lecture 3   RE NFA DFA
Lecture 3 RE NFA DFA
Rebaz Najeeb
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa
Sampath Kumar S
 

What's hot (20)

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Python: Manager Objects
Python: Manager ObjectsPython: Manager Objects
Python: Manager Objects
 
Python recursion
Python recursionPython recursion
Python recursion
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
 
Software Project Management( lecture 1)
Software Project Management( lecture 1)Software Project Management( lecture 1)
Software Project Management( lecture 1)
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERSVTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
VTU 3RD SEM UNIX AND SHELL PROGRAMMING SOLVED PAPERS
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Shell and its types in LINUX
Shell and its types in LINUXShell and its types in LINUX
Shell and its types in LINUX
 
Design Concept software engineering
Design Concept software engineeringDesign Concept software engineering
Design Concept software engineering
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
 
Cocomo model
Cocomo modelCocomo model
Cocomo model
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
Lecture 3 RE NFA DFA
Lecture 3   RE NFA DFA Lecture 3   RE NFA DFA
Lecture 3 RE NFA DFA
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa
 

Similar to SPL 11 | Nested Loops in C

ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
Mahyuddin8
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
week1 cs72 module 01 temp check 190.pptx
week1 cs72 module 01 temp check 190.pptxweek1 cs72 module 01 temp check 190.pptx
week1 cs72 module 01 temp check 190.pptx
hardmarcelia
 
Algorithms with-java-1.0
Algorithms with-java-1.0Algorithms with-java-1.0
Algorithms with-java-1.0
BG Java EE Course
 
Python.pptx
Python.pptxPython.pptx
Python Peculiarities
Python PeculiaritiesPython Peculiarities
Python Peculiarities
noamt
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
jamvantsolanki
 
03 control
03 control03 control
03 control
Massoud Hamad
 
12 doloops
12 doloops12 doloops
12 doloops
fyjordan9
 
Recursion
RecursionRecursion
Recursion
SAGARDAVE29
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
Flavia Tembo Kambale
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11
Paul Brebner
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
Exploring Tailrec Through Time Until Kotlin.pptx
Exploring Tailrec Through Time Until Kotlin.pptxExploring Tailrec Through Time Until Kotlin.pptx
Exploring Tailrec Through Time Until Kotlin.pptx
João Esperancinha
 
Week3
Week3Week3
Week3
준성 조
 
Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)
Software Craftsmanship Alicante
 
Introducción práctica a TDD
Introducción práctica a TDDIntroducción práctica a TDD
Introducción práctica a TDD
rubocoptero
 
Introduction to programming class 11 exercises
Introduction to programming   class 11 exercisesIntroduction to programming   class 11 exercises
Introduction to programming class 11 exercises
Paul Brebner
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
YashJain47002
 

Similar to SPL 11 | Nested Loops in C (20)

ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
week1 cs72 module 01 temp check 190.pptx
week1 cs72 module 01 temp check 190.pptxweek1 cs72 module 01 temp check 190.pptx
week1 cs72 module 01 temp check 190.pptx
 
Algorithms with-java-1.0
Algorithms with-java-1.0Algorithms with-java-1.0
Algorithms with-java-1.0
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Python Peculiarities
Python PeculiaritiesPython Peculiarities
Python Peculiarities
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
 
03 control
03 control03 control
03 control
 
12 doloops
12 doloops12 doloops
12 doloops
 
Recursion
RecursionRecursion
Recursion
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Introduction to programming - class 11
Introduction to programming - class 11Introduction to programming - class 11
Introduction to programming - class 11
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
Exploring Tailrec Through Time Until Kotlin.pptx
Exploring Tailrec Through Time Until Kotlin.pptxExploring Tailrec Through Time Until Kotlin.pptx
Exploring Tailrec Through Time Until Kotlin.pptx
 
Week3
Week3Week3
Week3
 
Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)
 
Introducción práctica a TDD
Introducción práctica a TDDIntroducción práctica a TDD
Introducción práctica a TDD
 
Introduction to programming class 11 exercises
Introduction to programming   class 11 exercisesIntroduction to programming   class 11 exercises
Introduction to programming class 11 exercises
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 

More from Mohammad Imam Hossain

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
BPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end examBPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end exam
sonukumargpnirsadhan
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Kalna College
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
Celine George
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
Payaamvohra1
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
RandolphRadicy
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
Celine George
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 

Recently uploaded (20)

Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
BPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end examBPSC-105 important questions for june term end exam
BPSC-105 important questions for june term end exam
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 
How to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in useHow to Fix [Errno 98] address already in use
How to Fix [Errno 98] address already in use
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
NIPER 2024 MEMORY BASED QUESTIONS.ANSWERS TO NIPER 2024 QUESTIONS.NIPER JEE 2...
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17How to Manage Reception Report in Odoo 17
How to Manage Reception Report in Odoo 17
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 

SPL 11 | Nested Loops in C

  • 1. Structured Programming Language Nested Loop Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2. Nested Loop • A nested loop is a loop within a loop i.e. an inner loop within the body of an outer one. • The first pass of the outer loop triggers the inner loop, which executes to completion. • Then the second pass of the outer loop triggers the inner loop again. • This repeats until the outer loop finishes.
  • 3. Nested for Loop #include <stdio.h> int main() { int n=3, m=5; int cnt=0,i,j; for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ ++cnt; } } printf("inner loop iterates in total %d timesn",cnt); return 0; } Trace it……
  • 4. Total no of Iterations • When i=1, the inner loop executes m times • When i=2, the inner loop executes m times • . • . • . • When i=n, the inner loop executes m times. So total no of iterations = m + m + m+ … … … +m = n *m
  • 5. Nested for Loop Trace it…… #include <stdio.h> int main() { int n=10; int cnt=0,i,j; for(i=1;i<=n;i++){ for(j=1;j<=i;j++){ ++cnt; } } printf("inner loop iterates in total %d timesn",cnt); return 0; }
  • 6. Total no of Iterations • When i=1, the inner loop executes 1 times • When i=2, the inner loop executes 2 times • When i=3, the inner loop executes 3 times • . • . • When i=n, the inner loop executes n times. So total no of iterations = 1 + 2 + 3+ … … … +n = n(n+1) / 2
  • 7. Flow Chart of Nested Loop
  • 8. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * * * * * * * * * * *
  • 9.
  • 10. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * *
  • 11. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * * * * * * * * * * *
  • 12. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * *
  • 13. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 14.
  • 15. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 16. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * *
  • 17. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 18. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 19. Problem • Input: n (integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * * * * * * * * * * *
  • 20. Problem • Input: n (integer) • Output: the following shape when n=5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
  • 21. Problem • Input: n (integer) • Output: the following shape when n=5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1
  • 22. Problem • Input: n (integer) • Output: the following shape when n=5 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5
  • 23. Problem • Input: n (integer) • Output: the following shape when n=4 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7
  • 24.
  • 25. References • https://www.programtopia.net/cprogramming/docs/ nested-loop • Number pyramids: http://www.codeforwin.in/2016/06/number- pattern-programs-in-c.html • Star pyramids: http://www.codeforwin.in/2015/07/star-patterns- program-in-c.html