Explore comprehensive Class 10 Computer Science Notes SEBA Chapter 5. Access concise summaries, key concepts, and essential information to enhance your understanding and excel in your studies.
NESTED LOOPS IN C
Exercise Questions
1. Write C programs to display the following pattern using nested loop construct.
i) 1 2 3
1 2 3
1 2 3
1 2 3
Ans:
#include <stdio.h>
int main() {
int rows = 5;
int cols = 3;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf(“%d “, j + 1);
}
printf(“\n”);
}
return 0;
}
ii) 1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
Ans:
#include <stdio.h>
int main() {
int rows = 5;
int cols = 3;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf(“%d “, j % 2 + 1);
}
printf(“\n”);
}
return 0;
}
iii) 4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
Ans:
#include <stdio.h>
int main() {
int rows = 4;
int cols = 4;
for (int i = 0; i < rows; i++) {
for (int j = cols; j >= 1; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
iv) 2
2 3 4
2 3 4 5 6
Ans:
#include <stdio.h>
int main() {
int rows, i, j, num;
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows – i; j++) {
printf(” “);
}
num = i + 1;
for (j = 1; j <= 2 * i – 1; j++) {
printf(“%d “, num++);
}
printf(“\n”);
}
return 0;
}
V) 1
1 2 1
1 2 3 2 1
Ans:
#include <stdio.h>
int main() {
int rows, i, j;
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf(“%d “, j);
}
for (j = i – 1; j >= 1; j–) {
printf(“%d “, j);
}
printf(“\n”);
}
return 0;
}
Vii) *
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Ans:
#include <stdio.h>
int main() {
int rows, i, j, space;
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);
for (i = 1; i <= rows; i++) {
for (space = 1; space <= rows – i; space++) {
printf(” “);
}
for (j = 1; j <= 2 * i – 1; j++) {
printf(“* “);
}
printf(“\n”);
}
for (i = rows – 1; i >= 1; i–) {
for (space = 1; space <= rows – i; space++) {
printf(” “);
}
for (j = 1; j <= 2 * i – 1; j++) {
printf(“* “);
}
printf(“\n”);
}
return 0;
}
Viii)
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Ans:
#include <stdio.h>
int main()
{
int i, j;
for(i = 5; i>= 1; i–)
{
for(j =1; j<=i; j++)
{
if (j <= i)
{
printf(“*”);
}
else
{
printf(” “);
}
}
printf(“\n”);
}
for (int i = 1; i <=4 ; i++) {
for (int j = 0; j <= i; j++) {
printf(“*”);
}
printf(“\n”);
}
return 0;
}
1 thought on “Class 10 Computer Science Notes SEBA Chapter 5”