Welcome to the ultimate study guide for SEBA Class 10 Computer Science Chapter 5 – Nested Loop in C Program.
Master all key concepts, syntax rules, and features of nested loop with our step-by-step solutions designed specifically for SEBA (Board of Secondary Education, Assam) students preparing for their HSLC examinations.
Question.1(a)
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Answer:-
#include<stdio.h>
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=3;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
Question. 1(b)
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
Answer:-
#include<stdio.h>
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=2;j++)
{
printf("%d",j);
}
printf("1");
printf("\n");
}
return 0;
}
Question. 1(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()
{
for(int i=1;i<=5;i++)
{ for(int j=4;j>=1;j–)
{
printf(“%d”,j);
}
printf(“\n”);
}
return 0;
}
Question. 1(d)
2
2 3 4
2 3 4 5 6
Answer:-
#include<stdio.h>
int main()
{
int a = 3;
int spaces = 2;
int count = 1;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= spaces; j++)
{
printf(" ");
}
int num = 2;
for (int k = 1; k <= count; k++)
{
printf("%d ", num);
num++;
}
printf("\n");
spaces--;
count = count + 2;
}
return 0;
}
Question. 1(e)
1
1 2 1
1 2 3 2 1
Answer:-
#include<stdio.h>
int main()
{
int a = 3;
int k = 2;
int m = 1;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= k; j++)
{
printf(" ");
}
int n = 1;
for (int x = 1; x <= m; x++)
{
printf("%d ", n);
n++;
}
n = n - 2;
for (int x = 1; x < m; x++)
{
printf("%d ", n);
n--;
}
printf("\n");
k--;
m++;
}
return 0;
}
Question. 1(f)
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Answer:-
#include<stdio.h>
int main()
{
int a = 7;
int k = 3;
int m = 1;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= k; j++)
{
printf(” “);
}
for (int x = 1; x <= m; x++)
{
printf("* ");
}
printf("\n");
if (i < 4)
{
k--;
m = m + 2;
} else
{
k++;
m = m - 2;
}
}
return 0;
}
Question. 1(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 a = 5;
int k = 12;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= i; j++)
{
printf(“x “);
}
for (int j = 1; j <= k; j++)
{
printf(" ");
}
for (int j = 1; j <= i; j++)
{
printf("x ");
}
printf("\n");
k = k - 3;
}
return 0;
}
Q. 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).
(a)
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Answer:-
#include
int main()
{
int lines;
printf(“Enter number of lines: “);
scanf(“%d”, &lines);
for(int i = 1; i <= lines; i++)
{
for(int 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
#include
int main()
{
int lines;
printf(“Enter number of lines: “);
scanf(“%d”, &lines);
for(int i = 1; i <= lines; i++)
{
for(int j = 1; j <= 2; j++)
{
printf("%d", j);
}
printf("1");
printf("\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
int main()
{
int lines;
printf(“Enter number of lines: “);
scanf(“%d”, &lines);
for(int i = 1; i <= lines; i++)
{
for(int j = 4; j >= 1; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
(d)
2
2 3 4
2 3 4 5 6
Answer:-
#include
int main()
{
int a;
printf(“Enter number of lines: “);
scanf(“%d”, &a);
int spaces = a - 1;
int count = 1;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= spaces; j++)
{
printf(" ");
}
int num = 2;
for (int k = 1; k <= count; k++)
{
printf("%d ", num);
num++;
}
printf("\n");
spaces--;
count = count + 2;
}
return 0;
}
(e)
1
1 2 1
1 2 3 2 1
Answer:-
#include
int main()
{
int a;
printf(“Enter number of lines: “);
scanf(“%d”, &a);
int k = a - 1;
int m = 1;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= k; j++)
{
printf(" ");
}
int n = 1;
for (int x = 1; x <= m; x++)
{
printf("%d ", n);
n++;
}
n = n - 2;
for (int x = 1; x < m; x++)
{
printf("%d ", n);
n--;
}
printf("\n");
k--;
m++;
}
return 0;
}
(f)
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Answer:-
#include
int main()
{
int a;
printf(“Enter number of lines (odd number): “);
scanf(“%d”, &a);
int mid = (a + 1) / 2;
int k = mid - 1;
int m = 1;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= k; j++)
{
printf(" ");
}
for (int x = 1; x <= m; x++)
{
printf("* ");
}
printf("\n");
if (i < mid)
{
k--;
m = m + 2;
}
else
{
k++;
m = m - 2;
}
}
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
#include
int main()
{
int a;
printf(“Enter number of lines: “);
scanf(“%d”, &a);
int k = (a - 1) * 3;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= i; j++)
{
printf("x ");
}
for (int j = 1; j <= k; j++)
{
printf(" ");
}
for (int j = 1; j <= i; j++)
{
printf("x ");
}
printf("\n");
k = k - 3;
}
return 0;
}
Q. 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:-
(a)
#include
int main()
{
int n, i, j, k;
printf("Enter number of lines: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
{
printf(" ");
}
for (k = 1; k < i * 2; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output
*
***
*****
*******
(b)
#include
int main()
{
int n, i, j, k;
printf("Enter number of lines: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = i; j < n; j++)
{
printf(" ");
}
for (k = 1; k < i * 2; k++)
{
printf("%d", k);
}
printf("\n");
}
return 0;
}
Output
1
123
12345
1234567
Q.4 what is a nested loop? Why do one use nested loops in our programs?
Answer :-
A nested loop is simply a loop written inside another loop. The loop on the outside is called the outer loop. The loop on the inside is called the inner loop. Every time the outer loop runs 1 time, the inner loop runs completely from start to finish.
Nested loops are useful when you need to work with multiple levels of data or repeat actions in a structured way. For example:
To print patterns like triangles or grids.
To work with tables, arrays, or lists of data.
To solve problems that involve comparing or combining items in pairs or groups.
In short, nested loops help us handle tasks where one process depends on repeating another process.
Q.5 Do we need to use same type of loops as outer and inner loops? Justify your answer with some code segments.
Answer:-
It’s not necessary to use the same type of loop in nested loops. For example, you can combine a for loop with a while loop or a do-while loop with a for loop.
Here’s an example:
#include
int main()
{
int i = 1;
while (i <= 3)
{
printf(“Outer loop iteration: %d\n”, i);
for (int j = 1; j <= 2; j++)
{
printf(” Inner loop iteration: %d\n”, j);
}
i++;
}
return 0;
}
Q. 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:-
You can add another loop inside the inner loop of a nested loop. This means you can create a third loop to make the nesting deeper.
#include
int main()
{
for (int i = 1; i <= 2; i++)
{
printf(“Outer loop iteration: %d\n”, i);
for (int j = 1; j <= 2; j++)
{
printf(” Inner loop iteration: %d\n”, j);
for (int k = 1; k <= 2; k++)
{
printf(” Third loop iteration: %d\n”, k);
}
}
}
return 0;
}
💻 Updated Solutions Notice: This page features complete, step-by-step SEBA Class 10 Computer Science Chapter 5 (Nested Loops in C) Textual Exercise Solutions updated for the 2026–27 academic session. All C programming solutions, nested for/while loop logic, and pattern generation programs strictly follow the latest revised Assam Board (SEBA) textbook.
💡 Ace Your HSLC Computer Science Exam:
- Have a doubt about inner vs. outer loop execution, printing star patterns, or tracing loop counts in Chapter 5? Leave your query in the comments section below!
- Save and bookmark this solution guide for fast reference before your unit tests, half-yearly, and pre-board examinations.
- Share this link with your classmates and WhatsApp study groups to help them master C programming!
Nice
Very helpful notes for SEBA