Class 10 Computer Science Chapter 4 | NESTED LOOPS IN C

Class 10 Computer Science Chapter 4 | NESTED LOOPS IN C: Welcome to our website! We are happy to provide you with Class 10th Notes for your academic journey. Class IX is an essential year for learners as they prepare themselves for the upcoming board exams, which will determine their academic success.

Today, I will discuss your Class 10th “NESTED LOOPS IN C” Long and Short Questions| We provide solutions for almost all long and short questions.

Our goal is to meet your needs. We provide the Notes here for free. We wish you all the best for your upcoming exams. If you have any doubts, don’t hesitate to get in touch with us.

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:

#include<stdio.h>

int main()

int i, j;

for(i=1;i<=5; i++)

{

for(j=1; j<=3;j++)

{

printf(“%d “j);

}

printf(“\n”);

}

return 0;

Output:

1 2 1

1 2 1

1 2 1

1 2 1

1 2 1

Process returned 0 (0x0) execution time: 0.311 s

Press any key to continue.

(c) 4 3 2 1

4 3 2 1

4 3 2 1

4 3 2 1

4 3 2 1

Solution:

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

4 3 2 1

4 3 2 1

4 3 2 1

4 3 2 1

4 3 2 1

Process returned 0 (0x0) execution time: 0.638 s

Press any key to continue.

(d)

2

2 3 4

2 3 4 5 6

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:

2

2 3 4

2 3 4 5 6

Process returned 0 (0x0) execution time: 0.504 s

Press any key to continue.

(e)

1

1 2 1

1 2 3 2 1

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:

1

1 2 1

1 2 3 1 2

Process returned 0 (0x0) execution time: 0.304 5

Press any key to continue.

(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<=k;j++)

{

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:

.

. . .

. . . . .

. . . . . . .

. . . . .

. . .

.

Process returned 0 (0x0) execution time: 0.620 s

Press any key to continue.

(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

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(” “);

}

for(i=1;i<=k; i++)

{

printf(” X “);

}

printf(“\n”);

}

return 0;

Output:

{

Process returned 0 (exe) execution time: 0.402 s

Press any key to continue.

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;

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

Enter the number of lines : 8

1 2 3

1 2 3

1 2 3

1 2 3

1 2 3

1 2 3

1 2 3

1 2 3

Process returned 0 (exe) execution time : 4.187 s

Press any key to continue.

(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);

Conclusion:

We believe these notes will help learners develop a better understanding of the topics and boost their confidence for their exams.

We are confident that these notes will help learners achieve their goals and enhance their academic performance. If you like this article and find it helpful, please share it with your friends.

Leave a Comment

error: Content is protected !!