SEBA class 10 computer science | chapter 5 : Nested loops in C | exercise and additional questions answers

SEBA class 10 computer science | chapter 5 : Nested loops in C | exercise and additional questions answers

SEBA class 10 computer science | chapter 5: Nested loops in C | exercise and additional questions answers are given below.

Exercise :

  1. Write C program 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

Ans: #include<stdio.h>

int main()
{

int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d\t”,j);

}
printf(“\n”);

}

return 0;

}

b. 1 2 1

1 2 1

1 2 1

1 2 1

1 2 1

Ans:

#include<stdio.h>

int main()
{

int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=2;j++)
{

printf(“%d\t”,j);

}
printf(“1\t”);

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

Ans: #include<stdio.h>

int main()
{

int i,j;
for(i=1;i<=5;i++) { for(j=4;j>=1;j–)
{
printf(“%d\t”,j);

}
printf(“\n”);
}

return 0;
}

d. 2

2 3 4

2 3 4 5 6

Ans: #include<stdio.h>

int main()
{

int i,j,k,m;
for(i=1;i<=3;i++)
{
for(j=1;j<=(3-i);j++)
{
printf(” \t”);
}

for(k=2;k<=2*i;k++)
{
printf(“%d\t”,k);

}

for(m=1;m<=(3-i);m++)
{
printf(” \t”);
}

printf(“\n”);

}

return 0;
}

e. 1

1 2 1

1 2 3 2 1

Ans: #include<stdio.h>

int main()
{
int i,j,m,k;
for(i=1;i<=3;i++)
{
for(j=1;j<=(3-i);j++)
{
printf(" \t");
}
for(m=1;m<=i;m++)
{

printf(“%d\t”,m);

}
for(k=(i-1);k>=1;k–)
{
printf(“%d\t”,k);
}
printf(“\n”);
}

return 0;
}

f.

                *

*     *     *

*     *     *     *    *

*    *    *    *      *     *     *

     *    *    *     *      *

*    *     *

*

Ans: #include<stdio.h>

int main()
{

int i,j,k,m,n,x;
for(i=1;i<=4;i++)
{
for(j=1;j<=(4-i);j++)
{

printf(” \t”);

}

for(k=1;k<=(2*i-1);k++)
{
printf(“X\t”);

}
printf(“\n”);
}
for(x=1;x<=3;x++)
{

for(m=1;m<=x;m++)
{
printf(” \t”);

}
for(n=7;n>=(x*2+1);n–)
{
printf(“X\t”);
}
printf(“\n”);
}

return 0;
}

g.

 *                                             *

*          *                                        *             *

*          *           *                            *             *             *

*         *           *        *                   *              *            *            *

*        *           *         *         *          *            *            *            *         *

Ans: #include<stdio.h>

int main()
{
int i,j,k,m,n;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{

printf(“X”);

}

for(k=4;k>=1;k–)
{

printf(” “);

}
for(j=1;j<=i;j++)
{

printf(“X”);

}
for(k=4;k>=1;k–)
{

printf(” “);
}

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)

Ans: 1. a. #include<stdio.h>

int main()
{

int n,i,j;
printf(“Enter the number of lines to be printed”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d\t”,j);

}
printf(“\n”);

}

return 0;

}

output:

  1. b. #include<stdio.h>

int main()
{

int n,i,j;
printf(“Enter the number of lines to be printed”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=2;j++)
{

printf(“%d\t”,j);

}
printf(“1\t”);

printf(“\n”);

}

return 0;
}

Output :

  1. c. #include<stdio.h>

int main()
{

int n,i,j;
printf(“Enter the number of lines to be printed”);
scanf(“%d”,&n);
for(i=1;i<=n;i++) { for(j=4;j>=1;j–)
{
printf(“%d\t”,j);

}
printf(“\n”);
}

return 0;
}

Output :

  1. d. #include<stdio.h>

int main()
{

int n,i,j,k,m;
printf(“Enter the number of lines to be printed”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i);j++)
{
printf(” \t”);
}

for(k=2;k<=2*i;k++)
{
printf(“%d\t”,k);

}

for(m=1;m<=(3-i);m++)
{
printf(” \t”);
}

printf(“\n”);

}

return 0;
}

Output :

  1. e. #include<stdio.h>

int main()
{
int n,i,j,m,k;
printf(“Enter the number of lines to be printed”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(3-i);j++)
{
printf(” \t”);
}
for(m=1;m<=i;m++)
{

printf("%d\t",m);

}
for(k=(i-1);k>=1;k–)
{
printf(“%d\t”,k);
}
printf(“\n”);
}

return 0;
}

Output:

  1. f. #include<stdio.h>

int main()
{

int a,b,i,j,k,m,n,x;
printf(“Enter the number of lines to be printed in the top pattern”);
scanf(“%d”,&a);
printf(“Enter the number of lines to be printed in the bottom pattern”);
scanf(“%d”,&b);
for(i=1;i<=a;i++)
{
for(j=1;j<=(4-i);j++)
{

printf(” \t”);

}

for(k=1;k<=(2*i-1);k++)
{
printf(“X\t”);

}
printf(“\n”);
}

for(x=1;x<=b;x++)
{

for(m=1;m<=x;m++)
{
printf(” \t”);

}
for(n=7;n>=(x*2+1);n–)
{
printf(“X\t”);
}
printf(“\n”);
}

return 0;
}

Output:



g. #include<stdio.h.

int main()
{
int a,i,j,k,m,n;
printf(“Enter the number of lines to be printed”);
scanf(“%d”,&a);
for(i=1;i<=a;i++)
{
for(j=1;j<=i;j++)
{

printf(“X”);

}

for(k=4;k>=1;k–)
{

printf(” “);

}
for(j=1;j<=i;j++)
{

printf(“X”);

}
for(k=4;k>=1;k–)
{

printf(” “);
}

printf(“\n”);
}

return 0;

}

Output :

Enter the number of lines to be printed 5
X    X
XX    XX
XXX    XXX
XXXX    XXXX
XXXXX    XXXXX

Process returned 0 (0x0)   execution time : 2.537 s
Press any key to continue.

Related Posts

  1. Chapter 1 : Introduction to Computer Network.
  2. Chapter 4 : Introduction to Loops.

Leave a Reply

Your email address will not be published. Required fields are marked *