Class 10 Computer Science Chapter 5 SEBA : Nested loops in C – Exercise Answers
SEBA Class 10 Computer Science Chapter 5 – Nested Loops in C: In this chapter, students learn about nested loops in C programming and how one loop can be placed inside another loop to solve different programming problems. This page provides exercise answers, solutions and explanations for Chapter 5 according to the SEBA Class 10 Computer Science syllabus.
These Class 10 Computer Science Nested Loops in C exercise answers are explained in a simple and student-friendly way to help learners understand the concepts, write C programs correctly and prepare for examinations.
SEBA Class 10 Computer Science Chapter 5: Nested Loops in C : Exercise Questions and Answers :
1. Write C programs to display the following patterns using nested loop construct.
| a. Print the following pattern: 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 Answer: #include<stdio.h> int main( ) { int k, a; for (k=1; k<=5; k++) { for(a=1; a<=3; a++) printf(“%d”, a); printf(“\n”); } return 0; } |
| a. Print the following pattern: 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 Answer: #include<stdio.h> int main( ) { int k, a, b; for (k=1; k<=5; k++) { for(a=1; a<=2; a++) printf(“%d”, a); for(b=1; b<=1; b++) printf(“%d”, b); printf(“\n”); } return 0; } |
| c. Print the following pattern: 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 Answer: #include<stdio.h> int main( ) { int k, a; for (k=1; k<=5; k++) { for(a=4; a>=1; a- -) printf(“%d”, a); printf(“\n”); } return 0; } |
| d. Print the following pattern: 2 2 3 4 2 3 4 5 6 Answer: #include<stdio.h> int main( ) { int k, a, b; for (k=1; k<=3; k++) { for(a=1; a<=(3-k); a++) printf(” “); for(b=2; b<=(2*k); b++) printf(“%d”, b); printf(“\n”); } return 0; } |
| e. Print the following pattern: 1 1 2 1 1 2 3 2 1 Answer: #include<stdio.h> int main( ) { int k, a, b, c; for (k=1; k<=3; k++) { for(a=1; a<=(3-k); a++) printf(” “); for(b=1; b<=k; b++) printf(“%d”, b); for(c= (k-1) ; c>=1 ; c- -) printf(“%d”, c); printf(“\n”); } return 0; } |
| f. Print the following pattern: * * * * * * * * * * * * * * * * * * * * * * * * * Answer: #include<stdio.h> int main( ) { int k, a, b, c; for(k=1 ; k<=4 ; k++) { for(a=1 ; a<=(4-k) ; a++) printf(” “); for(b=1 ; b<=(k*2-1); b++) printf(“*“); printf(“\n”); } for(k=1 ; k<=3 ; k++) { for(a=1 ; a<=k ; a++) printf(” “); for(b=5 ; b>=(2*k-1); b- -) printf(“*“); printf(“\n”); } return 0; } |
| g. Print the following pattern: 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 Answer: #include<stdio.h> int main( ) { int k, a, b ; for(k=1 ; k<=5 ; k++) { for(a=1 ; a<=k ; a++) printf(“x“); for(b=1 ; b<=(5-k); b++) printf(” “); for(a=1 ; a<=k ; a++) printf(“x“); printf(“\n”); } return 0; } |
| 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 (Hint write separate programs). Answer: Please find the modified program below of Q. 1 (a) :- #include<stdio.h> int main( ) { int k, a, N; printf(“Enter the value of N:”); scanf(“%d” , &N); for (k=1; k<=N; k++) { for(a=1; a<=3; a++) printf(“%d”, a); printf(“\n”); } return 0; } |
| 4. What is nested loop? Why do we use nested loops in our programs? Answer: Whenever we write a loop inside another loop, it is called a nested loop. A nested loop is divided into two parts: outer loop and inner loop. We use nested loops for the following reasons:- i) To reduce the length of the program. ii) To save time and effort. iii) To make the program easier and simpler than using separate loops. |
| 5. Do we need to use same type of loops as outer and inner loops? Justify your answer with some code segments. Answer: No, we don not need to use same type of loops as outer and inner loops. Some code segments are given below to justify: #include<stdio.h> int main( ) { int k, i ; for (k=1; k<=5; k++) { i=0 ; while(i<=3) { printf(“I read in class 10 \n”) ; i++ ; } printf(“\n”) ; } return 0 ; } On the above code, we used a for loop as the outer loop, and a while loop as the inner loop. Output of the code: I read in class 10 I read in class 10 I read in class 10 |
| 6. Do we need to use same type of loops as outer and inner loops? Justify your answer with some code segments. Answer: Yes, we can put third loop inside the inner loop of a nested loop construct. A program given below to justify: #include<stdio.h> int main( ) { int a, b, c ; for(a=1 ; a<=3; a++) { for(b=1 ; b<=3 ; b++) { for(c=1 ; c<=1 ; c++) printf(” X “) ; } printf(“\n”) ; } return 0 ; } Output of the program: X X X X X X X X X |
