0% found this document useful (0 votes)
2 views79 pages

Nested Loop

The document provides an overview of nested loops in C programming, including syntax and examples for nested for, while, and do-while loops. It illustrates various patterns that can be printed using nested loops, such as triangles and number sequences. Additionally, it includes programming exercises related to loops and other C programming concepts.

Uploaded by

tahasintonmoyo44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views79 pages

Nested Loop

The document provides an overview of nested loops in C programming, including syntax and examples for nested for, while, and do-while loops. It illustrates various patterns that can be printed using nested loops, such as triangles and number sequences. Additionally, it includes programming exercises related to loops and other C programming concepts.

Uploaded by

tahasintonmoyo44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Nested Loop

Nested
Loop
▪ Just like nested if else C allows the looping of statements inside another
loop
▪ There is no restriction for defining any number of loops. The nesting level
can be defined at n times.
▪ Example :
Outer_loop {
//outer loop statements…
Inner_loop {
// inner loop statements.
}
// outer loop statements.
}
Nested for loop
Syntax
for(initialization; condition; update loop variable)
{
// statement 1
………………………….

for(initialization; condition; update loop variable)


{
// Inner loop statements
}
// statement n
}
Nested while loop
Syntax
while(condition)
{
//outer loop statements.
while(condition)
{
// inner loop
statements.
}
// outer loop statements.
}
Nested do…while loop
Syntax
do {
// statements

do {
// Inner loop
statements
}while(condition);

// statements

}while(condition);
Nested for loop Example
Code
for(outVariable initialization ; condition; update outVariable)
{
// statement 1
outer
loop

………………………….
Inner
loop

for(inVariable initialization; condition; update inVariable) {


// Inner loop statements
}
// statement n
}
Nested for loop Example
Code
for(i=1; i<=2; i++) { Outer loop Inner loop
for(j=1; j<=2; j++) { Value of i condition Execution Value of j condition Execution
printf(“i:%d, ;1 1<=2, True Execute inner loop 1 1<=2,true i:1,j:1
2 2<=2,true i:1,j:2
j:%d\n”,i,j)
3 3<=2,false
} 2 2<=2,True Execute inner loop 1 1<=2,true i:2,j:1
2 2<=2,true i:2,j:2
} 3 3<=2,false
3 3<=2,False
Nested for loop Example 2(printing
stars)
for(i=1 ; i<=3 ; i++){ Sample Output
printf(“*”);
***
}
Nested for loop Example 3(printing
pattern) Sample Output
for(i=1 ; i<=3 ; i++){
***
for(j=1 ; j<=3; j++){ ***
***
printf(“*”);
} What will be the Output for this code?

*********
} How to solve?
Outer loop indicates/defines the row
Where to put the newline statement?
Inner loop indicated/defines the column
Nested for loop Example 3(printing
pattern) Sample Output
for(i=1 ; i<=3 ; i++){
***
for(j=1 ; j<=3; j++){ ***
printf(“*”); ***
}
printf(“\n”);
}
Outer loop indicates/defines the row
Inner loop indicated/defines the column
Nested for loop Example 4(printing
pattern) Take input of a number n and print a
for(i=1 ; i<=n ; i++){ pattern
Intput: n=3
for(j=1 ; j<=i; j++){ Sample Output
1
printf(“%d”,j) 12
; 123
Input: n=4
} Sample output
1
printf(“\n”); 12
123
}
1234
Nested for loop Example 5(printing
pattern) Take input of a number n and print a
for(i=1 ; i<=n ; i++){ pattern
for(j=0 ; j<i; j++){ Intput: n=3
Sample Output
printf(“%d”,n- 3
j);
32
} 321
Input: n=4
printf(“\n”); Sample output
4
}
43
432
4321
Nested for loop Example 5Alternate
Solution
for(i=1 ; i<=n ; i++){ Take input of a number n and print a
pattern
int printvalue=n;
Intput: n=3
for(j=0 ; j<i; j++){ Sample Output
3
printf(“%d”,
printvalue); 32
321
printvalue--; Input: n=4
Sample output
}
4
printf(“\n”); 43
} 432
4321
Nested for loop Example
6for(i=1;i<=n;i++) Take input of a number n and print a
{
for(j=1; j<=n-i;j++){ pattern
printf(" "); For printing space Intput: n=3
} Sample Output
1
1 2
for(k=1; k<=i ;k++){
For printing 1 2 3
printf("%d",k); numbers
}
printf("\n");
}
Nested for loop Example
7for(i=1;i<=n;i++) Take input of a number n and print a
{
for(j=1; j<=n-i;j++){ Triangle
printf(" "); For printing space Intput: n=3
} Sample Output
*
* * *
for(k=1; k<=2*i-1 ;k++){
For printing * * * * *
printf(“*"); stars
}
printf("\n");
}
Nested for loop Example
8for(i=1;i<=n;i++) Take input of a number n and print a
{
for(j=1; j<=n-i;j++){ Triangle
printf(" "); For printing space Intput: n=3
} Sample Output
1
1 2 3
for(k=1; k<=2*i-1 ;k++){
For printing 1 2 3 4 5
printf(“%d“,k); numbers
}
printf("\n");
}
Nested for loop Example
9for(i=1;i<=n;i++){ Take input of a number n and print a
for(j=1; j<=n-i;j++){ For Triangle
printf(" "); printing Intput: n=3
} space Sample Output
for(k=1; k<=i ;k++){ 1
printf("%d",k); For printing 1 2 1
} first
1 2 3 2 1
triangle
for(l=i ; l>1 ;l--){
printf("%d",l-1); For printing second
} triangle
printf("\n");
}
Nested for loop Sample
Problems
For input n = 3

1 2 3 2 1 1
1 2 1 2 3
2 4 5 6

1
1 2 1
1 2 3 2 1
Write a program in C to display the pattern.

1
2
3
4
Write a program in C to display the pattern.

11
22
33
44
Write a program in C to display the pattern like right angle
triangle with a number.

1
22
333
4444
i j

1 1
i j

1 1
1
i j

2 1
1
i j

2 1
1
2
i j

2 2
1
2
i j

2 2
1
22
i j

3 1
1
22
i j

3 1
1
22
3
i j

3 2
1
22
3
i j

3 2
1
22
33
i j

3 3
1
22
33
i j

3 3
1
22
333
i j

4 1
1
22
333
i j

4 1
1
22
333
4
i j

4 2
1
22
333
4
i j

4 2
1
22
333
44
i j

4 3
1
22
333
44
i j

4 3
1
22
333
444
i j

4 4
1
22
333
444
i j

4 4
1
22
333
4444
for(i=1; i<=4; i = i+1) for printing new lines

{
for(j=1;j<=2;j++){ for printing numbers in a line

printf(“%d”, i);}
printf(“\n”);
}
Write a program in C to display the pattern like right angle
triangle with a number.

1
12
123
1234
i j

1 1
1 j<=i
i j

2 1
1 j<=i
1
i j

2 2
1 j<=i
12
i j

3 1
1 j<=i
12
1
i j

3 2
1 j<=i
12
12
i j

3 3
1 j<=i
12
123
i j

4 1
1 j<=i
12
123
1
i j

4 2
1 j<=i
12
123
12
i j

4 3
1 j<=i
12
123
123
i j

4 4
1 j<=i
12
123
1234
for(i=1; i<=4; i = i+1) for printing new lines

{
for(j=1; j<=i; j++){ for printing numbers in a line

printf(“%d”, j);}
printf(“\n”);
}
Write a program in C to display the pattern like right angle
triangle with star.

*
**
***
****
i j

1 1
* j<=i
i j

2 1
* j<=i
*
i j

2 2
* j<=i
**
i j

3 1
* j<=i
**
*
i j

3 2
* j<=i
**
**
i j

3 3
* j<=i
**
***
i j

4 1
* j<=i
**
***
*
i j

4 2
* j<=i
**
***
**
i j

4 3
* j<=i
**
***
***
i j

4 4
* j<=i
**
***
****
for(i=1; i<=4; i = i+1) for printing new lines

{
for(j=1; j<=i; j++){ for printing numbers in a line

printf(“%d”, *);}
printf(“\n”);
}
Write a program in C to display the pattern.

*
***
*****
********
Write a program in C to display the pattern.

*
**
***
****
Write a program in C to display the pattern.

****
***
**
*
Write a program in C to display the pattern.

*
***
*****
*******
Write a program in C to display the pattern like right angle
triangle with a number.

*
***
*****
*******
*****
***
*
Write a program in C to display the pattern like right angle
triangle with a number.

*
**
* *
* *
* *
**
*
Write a program in C to display the pattern like right angle
triangle with a number.

1
1 2
1 2 3
1 2 3 4
Write a program in C to display the pattern like right angle
triangle with a number.

1
12
123
1234
Write a program in C to display the pattern like right angle
triangle with a number

1
23
456
7 8 9 10
Write a program in C to print the Floyd's Triangle.

1
01
101
0101
10101
1. Write a program in C to check whether a number is a palindrome or not.
2. Write a program in C to display the number in reverse order.
3. Write a program in C to sum the digits of a number.
4. Write a program in C to check Armstrong number of n digits.
5. Write a C program to check whether a number is a Strong Number or not.
6. Write a C program to determine whether a given number is prime or not.
7. Write a C program to determine prime number within a given range.
8. Write a C program to determine whether a number is perfect number or not.
9. Write a C program to determine the sum of a harmonic series.
[Link] a C program to determine the number of digits in a number.
Useful Study Link

[Link]
loop/[Link]

You might also like