SEBA Class 10 Computer Science Chapter 5 Nested Loops In C

Join Roy Library Telegram Groups

Hello Viewers Today’s We are going to Share With You, The Complete Syllabus of SEBA Class 10 Computer Science Chapter 5 Nested Loops In C Solutions in English in This Page. Are you a Student of (Secondary Education Board of Assam). SEBA Class 10 Computer Science Chapter 5 Nested Loops In C Notes. Which you Can Download PDF SEBA Class 10 Computer Science Chapter 5 Nested Loops In C Question Answer for free using direct Download Link Given Below in This Post.

SEBA Class 10 Computer Science Chapter 5 Nested Loops In C

We have Shared in This Post, SEBA Class 10 Computer Science Chapter 5 Nested Loops In C Textbook PDF Download for Free with you. SEBA Class 10 Computer Science Chapter 5 Nested Loops In C Solutions PDF. I Hope, you Liked The information About The SEBA Class 10 Computer Science Chapter 5 Nested Loops In C Book PDF. If you liked Class 10 Computer Science Notes Then Please Do Share this Post With your Friends as Well.

Nested Loops In C

TEXTUAL QUESTIONS AND ANSWERS

EXERCISE

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 i, j;for(i=1; i<=5; i++){for(j=1;j<=3j;++){printf(“%d “j);}printf(“\n”);}return(0);}

Output:

(b) 

1 2 1

1 2 1

1 2 1

1 2 1

1 2 1

Solution:

Code:

#include<stdio.h>int main(){int i, j;for(i=1; i<=5; i++){for(j=1; j<=2;j++){printf(“%d “,j);}for(j=1;j>=1;j—){printf(“%d”,j);}printf(“\n”);}return(0);}

Output:

(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 i, j;for(i=1; i<=5; i++){for(j=4; j>=1;j—){printf(“%d “,j);}printf(“\n”);}return(0);}

Output:

(d) 

    2

  234

23456

Solution:

Code:

#include<stdio.h>int main(){int i, j, k, m=2;for(i=3; i>=1; i—){for(j=1; j<i;j++){printf(” “,j);}for(k=2;k<=m;k++){printf(“%d”,k);}m = m + 2;printf(“\n”);}return(0);}

Output:

(e) 

    1

  121

12321

Solution:

Code:

#include<stdio.h>int main(){int i, j, k, p;for(k=1;k<=3;k++){for(i=1;i<=3-k;i++){printf(” “);}for(j=1;j<=k;j++){printf(” %d “,j);}for(p=1;p<=k-1;p++){printf(” %d “,p);}printf(“\n”);}return 0;}

Output:

(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(” “);}for(j=1:j<=kj++){printf(” * “);}for(p=1;p<=k-1;p++){printf(” * “);}printf(“\n”);}for(k=3;k>=1;k—){for(i=1;i<=4-k;i++){printf(” “);}for(j=1;j<=k;j++){printf(” * “);}for(p=1;p<=k-1;p++){printf(” * “);}printf(“\n”);}return 0;}

Output:

(g) 

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(” “);}P—;for(i=1;i<=k; i++){printf(” X “);}printf(“\n”);}return 0;}

Output:

S.L No.CONTENTS
Chapter 1Introduction To Computer Network
Chapter 2Html5 And Css3
Chapter 3Database Part – II Mysql
Chapter 4Introduction To Loops
Chapter 5Nested Loops In C
Chapter 6Arrays In C
Chapter 7Functions In C
Chapter 8Pointers In C
Chapter 9Structure In C
Chapter 10An Introduction To Object Oriented Programming
Chapter 11Case 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:

(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=1; j>=1;j—){printf(“%d”,j);}printf(“\n”);}return(0);}

Output:

(c) 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=4; j>=1;j—){printf(“%d “,j);}printf(“\n”);}return(0);}

Output:

(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(” “,j);}for(k=2;k<=m;k++){printf(“%d”,k);}m = m + 2;printf(“\n”);}return(0);}

Output:

(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=1;p<=k-1;p++){printf(” %d “.p);}printf(“\n”);}return 0;}

Output:

(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:

(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:

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

Solution: Example 5.6

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<=(2*k-1); j++){printf(” X “);}printf(“\n”);}return 0;}

Output:

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:

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

Ans. A nested loop has one loop inside of another. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop.

Nesting of loops is the feature in C that allows the looping of statements inside another loop. It is useful when we are dealing with more number of iterations, the memory size usage will be reduced.

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 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 i, j;for(i=1;i<=3;i++){j=1;while(j<=10){printf(“%d\t”,(i*))):j++;}printf(“\n”);}return 0;}

Output:

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

Ans. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started. The inner loop must finish all of its iterations before the outer loop can continue to its next iteration.

Example:

Code:

#include <stdio.h>int main(){int i, j, k;for(i=1;i<=3;i++){j=1;while(j<=5){for(k=1;k<=3;k++){printf(“%d\t”.(i*))):}printf(“\n”);j++;}printf(“\n”);}return 0;}

Output:

ADDITIONAL QUESTIONS AND ANSWERS

1. What is nested loop?

Ans. A nested loop has one loop inside of another. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop.

2. What kind of loops can be nested?

Ans. The most common applications of loops are for matrix data (e.g., looping through the rows and columns of a table). We can nest any type of loop inside any other type, a for loop can be nested in a while loop or while loop can be nested in a do..while loop.

3. How will we terminate outer loop from a block of the inner loop?

Ans. In order to terminate the outer loop from the inner block, we need to use a break statement.

4. What is an infinite loop?

Ans. An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an indefinite loop or an endless loop.

5. What is inner and outer loop?

Ans. A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.

Leave a Reply

error: Content is protected !!
Scroll to Top