Get answers, questions, notes, textbook solutions, extras, pdf, mcqs for Computer chapter 5 Nested loops in C of class 10 (HSLC/Madhyamik) for students studying under the Board of Secondary Education, Assam (SEBA). These notes/answers, however, should only be used for references and modifications/changes can be made wherever possible.
Summary
The chapter titled “Nested loops in C” provides an understanding of the concept of nested loops in C programming. It begins with an introduction to nested loops, explaining that a nested loop is a loop placed inside another loop. The outer loop is referred to as the ‘outer loop’, and the loop inside it is called the ‘inner loop’. It emphasizes that nested loops are a powerful tool in programming, simplifying complex tasks and reducing the length of the program.
It delves into practical applications of nested loops, demonstrating how to replace all occurrences of a given element in an array, calculate the summation of array elements, and display patterns using nested loops. It also explains how to take an array as input from the user and search for a number in a series of numbers.
Further, it explores the concept of a flag variable in a search operation in an array, which is used to indicate whether a match was found. It also discusses the use of the index variable in a search operation to keep track of the current element.
Video tutorial
Textual questions and answers
1. Write C programs to display the following patterns using nested loop construct.
a.
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Answer:
#include <stdio.h>
int main() {
int i, j;
for(i = 0; i < 5; i++) {
for(j = 1; j <= 3; j++) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
b.
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
Answer:
#include <stdio.h>
int main() {
int i, j;
for(i = 0; i < 5; i++) {
for(j = 1; j <= 2; j++) {
printf(“%d “, j);
}
printf(“1\n”);
}
return 0;
}
c.
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 i, j;
for(i = 0; i < 5; i++) {
for(j = 4; j >= 1; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
d.
2
2 3 4
2 3 4 5 6
Answer:
#include <stdio.h>
int main() {
int i, j, k;
for(i = 2; i <= 5; i++) {
for(k = 5; k > i; k–) {
printf(” “);
}
for(j = 2; j <= i; j++) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
e.
1
1 2 1
1 2 3 2 1
Answer:
#include <stdio.h>
int main() {
int i, j, k;
for(i = 1; i <= 3; i++) {
for(k = 3; k > i; k–) {
printf(” “);
}
for(j = 1; j <= i; j++) {
printf(“%d “, j);
}
for(j = i-1; j >= 1; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
f.
*
***
*****
*******
*****
***
*
Answer:
#include <stdio.h>
int main() {
int i, j, k;
for(i = 1; i <= 4; i++) {
for(k = 4; k > i; k–) {
printf(” “);
}
for(j = 1; j <= (2*i-1); j++) {
printf(“*”);
}
printf(“\n”);
}
for(i = 3; i >= 1; i–) {
for(k = 4; k > i; k–) {
printf(” “);
}
for(j = 1; j <= (2*i-1); j++) {
printf(“*”);
}
printf(“\n”);
}
return 0;
}
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 x x
Answer:
#include <stdio.h>
int main() {
int i, j, k;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf(“x “);
}
for(k = 10; k > (2*i); k–) {
printf(” “);
}
for(j = 1; j <= i; j++) {
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: (a)
#include <stdio.h>
int main() {
int i, j, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for(i = 0; i < lines; i++) {
for(j = 1; j <= 3; j++) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
(b)
#include <stdio.h>
int main() {
int i, j, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for(i = 0; i < lines; i++) {
for(j = 1; j <= 2; j++) {
printf(“%d “, j);
}
printf(“1\n”);
}
return 0;
}
(c)
#include <stdio.h>
int main() {
int i, j, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for(i = 0; i < lines; i++) {
for(j = 4; j >= 1; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
(d)
#include <stdio.h>
int main() {
int i, j, k, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for(i = 2; i <= lines+1; i++) {
for(k = lines+1; k > i; k–) {
printf(” “);
}
for(j = 2; j <= i; j++) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
(e)
#include <stdio.h>
int main() {
int i, j, k, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for(i = 1; i <= lines; i++) {
for(k = lines; k > i; k–) {
printf(” “);
}
for(j = 1; j <= i; j++) {
printf(“%d “, j);
}
for(j = i-1; j >= 1; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
(f)
#include <stdio.h>
int main() {
int i, j, k, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for(i = 1; i <= lines; i++) {
for(k = lines; k > i; k–) {
printf(” “);
}
for(j = 1; j <= (2*i-1); j++) {
printf(“*”);
}
printf(“\n”);
}
for(i = lines-1; i >= 1; i–) {
for(k = lines; k > i; k–) {
printf(” “);
}
for(j = 1; j <= (2*i-1); j++) {
printf(“*”);
}
printf(“\n”);
}
return 0;
}
(g)
#include <stdio.h>
int main() {
int i, j, k, lines;
printf(“Enterthe number of lines: “);
scanf(“%d”, &lines);
for(i = 1; i <= lines; i++) {
for(j = 1; j <= i; j++) {
printf(“x “);
}
for(k = 2*lines; k > (2*i); k–) {
printf(” “);
}
for(j = 1; j <= i; j++) {
printf(“x “);
}
printf(“\n”);
}
return 0;
}
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.
Answer: 5.6 modified
#include <stdio.h>
int main()
{
int i, j, k, p, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for (k = 1; k <= lines; k++)
{
for(i = 1; i <= (lines – k); i++)
{
printf(” “);
}
for(j = 1; j <= (2*k – 1); j++)
{
printf(“X”);
}
printf(“\n”);
}
return 0;
}
5.7 modified:
#include <stdio.h>
int main()
{
int i, j, k, lines;
printf(“Enter the number of lines: “);
scanf(“%d”, &lines);
for (k = 1; k <= lines; k++)
{
for(i = 1; i <= (lines – k); i++)
{
printf(” “);
}
for(j = 1; j <= (2*k – 1); j++)
{
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
4. What is a nested loop? Why do one use nested loops in our programs?
Answer: A nested loop is a loop that is placed inside another loop, often referred to as the outer loop. The loop inside is called the inner loop.
Nested loops are used in programming to simplify complex tasks and reduce the length of the program. They are particularly useful when each iteration of the outer loop needs to be combined with each iteration of the inner loop, such as when working with multi-dimensional arrays or when creating certain patterns.
5. Do we need to use same type of loops as outer and inner loops? Justify your answer with some code segments.
Answer: It is not necessary to use the same type of loops as outer and inner loops. You can use any combination of for, while, and do-while loops as outer and inner loops based on the requirements of your program. For example, you can have a for loop as the outer loop and a while loop as the inner loop, or vice versa. The choice of loop depends on the specific needs of the task you’re trying to accomplish.
Example:
for(int i = 0; i < 5; i++) {
int j = 0;
while(j < 5) {
printf(“%d “, j);
j++;
}
printf(“\n”);
}
6. Can we put a third loop inside the inner loop of a nested loop constract? Write a C program to justify your answer.
Answer: Yes, you can put a third loop inside the inner loop of a nested loop construct. This is often referred to as multi-level nested looping. Here is a simple C program that demonstrates this:
#include <stdio.h>
int main() {
int i, j, k;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
for(k = 0; k < 3; k++) {
printf(“%d %d %d\n”, i, j, k);
}
}
}
return 0;
}
In this program, there are three levels of nested loops. The outermost loop runs three times, and for each iteration of the outermost loop, the middle loop also runs three times. For each iteration of the middle loop, the innermost loop runs three times. This results in a total of 27 iterations (333).
Additional/extra questions and answers
1. How can you replace all occurrences of a given element in an array in C? Provide a code segment to illustrate this.
Answer: To replace all occurrences of a given element in an array in C, you can iterate over the array and compare each element with the given element. If they match, you replace the element in the array with the new value. Here’s a code segment that illustrates this:
int old_num, new_num;
for(int i = 0; i < totalEle; i++) {
if(number[i] == old_num) {
number[i] = new_num;
}
}
2. How can you search for a number in a series of numbers in C? Provide a code segment to illustrate this.
Answer: To search for a number in a series of numbers in C, you can iterate over the array and compare each element with the target number. If they match, you can print the index or do whatever is required. Here’s a code segment that illustrates this:
int target;
for(int i = 0; i < totalEle; i++) {
if(number[i] == target) {
printf(“The number is found at index %d\n”, i);
}
}
3. How can you calculate the summation of array elements in C? Provide a code segment to illustrate this.
Answer: To calculate the summation of array elements in C, you can initialize a sum variable to 0 and then iterate over the array, adding each element to the sum. Here’s a code segment that illustrates this:
int sum = 0;
for(int i = 0; i < totalEle; i++) {
sum += number[i];
}
printf(“The sum of the array elements is: %d\n”, sum);
4. How can you display a pattern of ‘X’ characters in increasing and decreasing order in C? Provide a code segment to illustrate this.
Answer: To display a pattern of ‘X’ characters in increasing and decreasing order in C, you can use nested loops. The outer loop controls the number of lines, and the inner loop controls the number of ‘X’ characters on each line. Here’s a code segment that illustrates this:
for(int i = 5; i >= 1; i–) {
for(int j = 1; j <= i; j++) {
printf(“X”);
}
printf(“\n”);
}
for(int i = 2; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
printf(“X”);
}
printf(“\n”);
}
5. How can you count the number of occurrences of a given number in an array in C? Provide a code segment to illustrate this.
Answer: To count the number of occurrences of a given number in an array in C, you can iterate over the array and increment a counter each time you encounter the given number. Here’s a code segment that illustrates this:
int target, count = 0;
for(int i = 0; i < totalEle; i++) {
if(number[i] == target) {
count++;
}
}
printf(“The number %d occurs %d times in the array.\n”, target, count);
6. How can you take an array as input from the user in C? Provide a code segment to illustrate this.
Answer: To take an array as input from the user in C, you can use a loop that iterates for the number of elements in the array, and use scanf to read each element. Here’s a code segment that illustrates this:
printf(“Enter the number of elements: “);
scanf(“%d”, &totalEle);
printf(“Enter the elements: “);
for(int i = 0; i < totalEle; i++) {
scanf(“%d”, &number[i]);
}
Additional/extra MCQs
1. What is the purpose of a flag variable in a search operation in an array?
a) To keep track of the maximum value
b) To keep track of the minimum value
c) To indicate whether a match was found
d) To count the number of elements
Answer: c) To indicate whether a match was found
2. How can you replace all occurrences of a given element in an array in C?
a) By using a while loop
b) By using a for loop
c) By using a do-while loop
d) Both a and b
Answer: d) Both a and b
3. How can you calculate the summation of array elements in C?
a) By using a while loop
b) By using a for loop
c) By using a do-while loop
d) Both a and b
Answer: d) Both a and b
4. How can you display a pattern of ‘X’ characters in increasing and decreasing order in C?
a) By using a single for loop
b) By using a single while loop
c) By using nested for loops
d) By using nested while loops
Answer: c) By using nested for loops
5. How can you take an array as input from the user in C?
a) By using a while loop
b) By using a for loop
c) By using a do-while loop
d) Both a and b
Answer: d) Both a and b
6. How can you search for a number in a series of numbers in C?
a) By using a while loop
b) By using a for loop
c) By using a do-while loop
d) Both a and b
Answer: d) Both a and b
7. What is the purpose of the index variable in a search operation in an array?
a) To keep track of the current element
b) To indicate whether a match was found
c) To count the number of elements
d) To keep track of the maximum value
Answer: a) To keep track of the current element
Ron’e Dutta is a journalist, teacher, aspiring novelist, and blogger. He manages Online Free Notes and reads Victorian literature. His favourite book is Wuthering Heights by Emily Bronte and he hopes to travel the world. Get in touch with him by sending him a friend request.
Get notes of other boards, classes, and subjects