Final Exam Offer HSLC & HS All-in-One PDF Notes @ ₹299 ₹599 50% OFF
Ends:
03d
14h
35m
40s

Class 10 Computer Science Chapter 5 Nested Loops In C

Class 10 Computer Science Chapter 5 Nested Loops In C

TEXTUAL QUESTIONS AND ANSWERS

1. Write C programs to display the following pattern using nested loop construct. 

(a) 1 2 3 

1 2 3

1 2 3

1 2 3

1 2 3

Solution: Code:

#include <stdio.h>

int main() {

    int rows, cols;

    printf("Enter the number of rows: ");

    scanf("%d", &rows);

    printf("Enter the number of columns: ");

    scanf("%d", &cols);

    for (int i = 1; i <= rows; i++) {

        for (int j = 1; j <= cols; j++) {

            printf("%d ", j);

        }

        printf("\n");

    }

    return 0;

}

Output: 

Enter the number of rows: 5

Enter the number of columns: 3

1 2 3 

1 2 3 

1 2 3 

1 2 3 

1 2 3

(b) 1 2 1 

1 2 1 

1 2 1 

1 2 1 

1 2 1 

Solution:

Code:

#include <stdio.h>

int main() {

    int rows, cols;

    printf("Enter the number of rows: ");

    scanf("%d", &rows);

    printf("Enter the number of columns: ");

    scanf("%d", &cols);

    for (int i = 1; i <= rows; i++) {

        for (int j = 1; j <= cols; j++) {

            printf("%d ", j % 2 + 1);

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of rows: 5

Enter the number of columns: 3

1 2 1 

1 2 1 

1 2 1 

1 2 1 

1 2 1

(c) 4 3 2 1

4 3 2 1

4 3 2 1

4 3 2 1 

4 3 2 1

Solution: Code:

#include <stdio.h>

int main() {

    int rows, cols;

    printf("Enter the number of rows: ");

    scanf("%d", &rows);

    printf("Enter the number of columns: ");

    scanf("%d", &cols);

    for (int i = 1; i <= rows; i++) {

        for (int j = cols; j >= 1; j--) {

            printf("%d ", j);

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of rows: 5

Enter the number of columns: 4

4 3 2 1 

4 3 2 1 

4 3 2 1 

4 3 2 1 

4 3 2 1

(d)                2

                 2  3  4

              2 3 4 5 6

Solution: Code: 

#include <stdio.h>

int main() {

    int rows;

    printf("Enter the number of rows: ");

    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {

        // Print spaces

        for (int space = 1; space <= rows - i; space++) {

            printf("  ");

        }

        // Print numbers

        int num = i + 1;

        for (int j = 1; j <= i; j++) {

            printf("%d ", num);

            num++;

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of rows: 3

  2 

 2 3 4 

2 3 4 5 6

(e)

1

1 2 1

1 2 3 2 1

Solution: Code:

#include <stdio.h>

int main() {

    int rows;

    printf("Enter the number of rows: ");

    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {

        // Print spaces

        for (int space = 1; space <= rows - i; space++) {

            printf("  ");

        }

        // Print increasing numbers

        for (int j = 1; j <= i; j++) {

            printf("%d ", j);

        }

        // Print decreasing numbers

        for (int k = i - 1; k >= 1; k--) {

            printf("%d ", k);

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of rows: 3

  1 

 1 2 1 

1 2 3 2 1

(f) 

      * 

    * * * 

  * * * * * 

* * * * * * * 

  * * * * * 

    * * * 

      *

Solution:

Code:

#include <stdio.h>

int main() {

    int i, j, k, p;

    for (k = 1; k <= 4; k++) {

        for (i = 1; i <= 4 - k; i++) {

            printf("  ");  // Print spaces

        }

        for (j = 1; j <= k; j++) {

            printf(" * ");  // Print asterisks

        }

        for (p = 1; p <= k - 1; p++) {

            printf(" * ");  // Print additional asterisks

        }

        printf("\n");

    }

    for (k = 3; k >= 1; k--) {

        for (i = 1; i <= 4 - k; i++) {

            printf("  ");  // Print spaces

        }

        for (j = 1; j <= k; j++) {

            printf(" * ");  // Print asterisks

        }

        for (p = 1; p <= k - 1; p++) {

            printf(" * ");  // Print additional asterisks

        }

        printf("\n");

    }

    return 0;

}

Output:

      * 

    * * * 

  * * * * * 

* * * * * * * 

  * * * * * 

    * * * 

      *

(g) 

X             x 

 X X         x x 

 X X X     x x x 

 X X X X x x x x 

 X X X X X X X X

Solution: Code:

#include <stdio.h>

int main() {

    int i, j, k, p = 4;

    for (k = 1; k <= 5; k++) {

        for (i = 1; i <= k; i++) {

            printf(" X ");

        }

        for (j = 1; j <= p; j++) {

            printf("   ");  // Print spaces

        }

        p--;

        for (i = 1; i <= k; i++) {

            printf(" x ");

        }

        printf("\n");

    }

    return 0;

}

Output:

 X             x 

 X X         x x 

 X X X     x x x 

 X X X X x x x x 

 X X X X X X X X

Computer Science Textual Q&A

SL. No. Contents
Chapter – 1 Introduction To Computer Network
Chapter – 2 HTML5 AND CSS3
Chapter – 3 DATABASE PART – II MYSQL
Chapter – 4 INTRODUCTION TO LOOPS
Chapter – 5 NESTED LOOPS IN C
Chapter – 6 ARRAYS IN C
Chapter – 7 FUNCTIONS IN C
Chapter – 8 POINTERS IN C
Chapter – 9 STRUCTURE IN C
Chapter – 10 AN INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Chapter – 11 CASE STUDIES

2. Modify the solution of question no. 1 to accept the number of lines as the input. The program should make the display pattern accordingly.

Solution:

(a) Code:

#include<stdio.h>

int main()

{

int i, j, n;

printf(“Enter the number of lines : “);

scanf(“%d”, &n);

for(i=1; i<=n ; i++)

{

for(j=1 ; j<=3; j++)

{

printf(“%d “,j);

}

printf(“\n”);

}

return(0);

}

Output:

Enter the number of lines : 5

1 2 3

1 2 3

1 2 3

1 2 3

1 2 3

(b) Code: 

#include <stdio.h>

int main()

{

    int i, j, n;

    printf("Enter the number of lines : ");

    scanf("%d", &n);

    for (i = 1; i <= n; i++)

    {

        for (j = 1; j <= 2; j++)

        {

            printf(" %d ", j);

        }

        for (j = 2; j >= 1; j--)

        {

            printf(" %d", j);

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of lines : 3

1 2 2 1

1 2 2 1

1 2 2 1

(c) Code:

#include<stdio.h>

int main()

{

int i, j, n;

printf(“Enter the number of lines : “);

scanf(“%d “,j);

}

printf(“\n”);

}

return(0);

}

Output: 

Enter the number of lines: 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

(d) Code:

#include <stdio.h>

int main()

{

    int i, j, k, m = 2, n;

    printf("Enter the number of lines: ");

    scanf("%d", &n);

    for (i = n; i >= 1; i--)

    {

        for (j = 1; j < i; j++)

        {

            printf(" ");

        }

        for (k = 2; k <= m; k++)

        {

            printf(" %d", k);

        }

        m = m + 2;

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of lines: 8

        2 4 6 8 10 12 14 16

         2 4 6 8 10 12 14

          2 4 6 8 10 12

           2 4 6 8 10

            2 4 6 8

             2 4 6

              2 4

               2

(e) Code: 

#include <stdio.h>

int main()

{

    int i, j, k, p, n;

    printf("Enter the number of lines: ");

    scanf("%d", &n);

    for (k = 1; k <= n; k++)

    {

        for (i = 1; i <= n - k; i++)

        {

            printf(" ");

        }

        for (j = 1; j <= k; j++)

        {

            printf(" %d ", j);

        }

        for (p = k - 1; p >= 1; p--)

        {

            printf(" %d ", p);

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of lines: 8

       1 

      1 2 1 

     1 2 3 2 1 

    1 2 3 4 3 2 1 

   1 2 3 4 5 4 3 2 1 

  1 2 3 4 5 6 5 4 3 2 1 

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

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

(f) Code:

#include <stdio.h>

int main()

{

    int i, j, k, p, a, n, b;

    printf("Enter the odd number of lines: ");

    scanf("%d", &n);

    a = n / 2 + 1;

    b = a - 1;

    for (k = 1; k <= a; k++)

    {

        for (i = 1; i <= a - k; i++)

        {

            printf(" ");

        }

        for (j = 1; j <= k; j++)

        {

            printf(" * ");

        }

        for (p = 1; p <= k - 1; p++)

        {

            printf(" * ");

        }

        printf("\n");

    }

    for (k = b; k >= 1; k--)

    {

        for (i = 1; i <= a - k; i++)

        {

            printf(" ");

        }

        for (j = 1; j <= k; j++)

        {

            printf(" * ");

        }

        for (p = 1; p <= k - 1; p++)

        {

            printf(" * ");

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the odd number of lines: 7

      * 

     * * * 

    * * * * * 

   * * * * * * * 

  * * * * * * * * * 

 * * * * * * * * * * * 

* * * * * * * * * * * * * 

 * * * * * * * * * * * 

  * * * * * * * * * * 

   * * * * * * * * 

    * * * * * * * 

     * * * * * * 

      * * * * * 

       * * * * 

        * * * 

         * * 

          *

(g) Code:

#include<stdio.h>

int main()

{

int i, j, k, p, n;

printf(“Enter the number of lines : “);

scanf(“%d”, &n);

p = n-1;

for(k=1; k<=n; k++)

{

for(i=1; i<=k; i++)

{

printf(“ X “);

}

for(j=1; j<=p; j++)

{

printf(“ “);

}

p—;

for(i=1; i<=k; i++)

{

printf(“ X “);

}

printf(“\n”),

}

return 0;

}

Output:

Enter the number of lines: 4

 X     X 

 X X   X X 

 X X X X X X 

 X X   X X

3. Extend the programs of Example 5.6 and Example 5.7 to make it dynamic by accepting the number of lines as input from the keyboard.

Solution: Example 5.6: Code: 

#include <stdio.h>

int main()

{

    int i, j, k, n;

    printf("Enter the number of lines: ");

    scanf("%d", &n);

    for (k = 1; k <= n; k++)

    {

        for (i = 1; i <= (n - k); i++)

        {

            printf(" ");

        }

        for (j = 1; j <= (2 * k - 1); j++)

        {

            printf(" X ");

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of lines: 10

         X         

        X X        

       X X X       

      X X X X      

     X X X X X     

    X X X X X X    

   X X X X X X X   

  X X X X X X X X  

 X X X X X X X X X 

X X X X X X X X X X

Example: 5.7: Code: 

#include <stdio.h>

int main()

{

    int i, j, k, n;

    printf("Enter the number of lines: ");

    scanf("%d", &n);

    for (k = 1; k <= n; k++)

    {

        for (i = 1; i <= (n - k); i++)

        {

            printf(" ");

        }

        for (j = 1; j <= (2 * k - 1); j++)

        {

            printf(" %d ", j);

        }

        printf("\n");

    }

    return 0;

}

Output:

Enter the number of lines: 4

   1   

  1 2 3  

 4 5 6 7 8 

9 10 11 12 13 14 15

4. What is nested loop? Why do one use nested loops in our programs?

Ans. A nested loop is a loop inside another loop. Although all kinds of loops can be nested, the most common nested loop involves loops.

These loops are particularly useful when displaying multidimensional data. When using these loops, the first iteration of the first loop will initialize, followed by the second loop.

Example: Any number of loops can be defined inside another loop, i, e., There is no restriction for defining any number of loops. The nesting level can be defined at n times.

5. Do we need to use same type of loops as outer and inner loops? Justify your answer with some code segments.

Ans. It is not mandatory to nest the same type of loop. We can put a for loop inside a while loop or a do-while loop inside a for loop.

Example: Code:

#include <stdio.h>

int main() {

    int rows = 5;

    for (int i = 1; i <= rows; i++) {

        int j = 1;

        while (j <= i) {

            printf("%d ", j);

            j++;

        }

        printf("\n");

    }

    return 0;

}

Output:

1 

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5

6. Can we put third loop inside the inner loop of a nested loop construct? Write a C program to justify your answer.

Ans. Yes, you can place a third loop inside the inner loop of a nested loop construct.

Example:

Code:

#include <stdio.h>

int main() {

    int i, j, k;

    for (i = 1; i <= 3; i++) {

        for (j = 1; j <= 3; j++) {

            for (k = 1; k <= 3; k++) {

                printf("%d %d %d\n", i, j, k);

            }

        }

    }

    return 0;

}

Output:

1 1 1

1 1 2

1 1 3

1 2 1

1 2 2

1 2 3

1 3 1

1 3 2

1 3 3

2 1 1

2 1 2

2 1 3

2 2 1

2 2 2

2 2 3

2 3 1

2 3 2

2 3 3

3 1 1

3 1 2

3 1 3

3 2 1

3 2 2

3 2 3

3 3 1

3 3 2

3 3 3

Conclusion:

Class 10 Computer Science Chapter 5 Nested Loops In C Textual Questions and Answers will help students understand the chapter’s key concepts and prepare for their exams.

NEW VIDEO CLASS

Subject Ka Complete Video Solution Dekhein

Step-by-step easy concept explanation aur full questions Answer ke liye watch karein ya app download karein:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top