Вы находитесь на странице: 1из 4

More application nested loops:

C program using nested loops (Serial numbers just to show you can study the logic of the program).
Example-1. Write a program in C using nested loops which has an output of 1-8 (newline) 2-9 (newline)
3-10 (newline) 4-11(newline) 5-12

1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int
{
int
int
int

main(void)
a = 1;
b = 0;
c = 8;

8.

d
e
v

9.
10.

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


{

11.
12.
13.
14.

o
r
p

for(b = a; b <= c; b++)


{
printf("%d", b);
}

15.
16.
17.

p
A
18.
19.
20.

c++;
printf('\n');
}

printf("\n");
return 0;

Is the output expected to look like this?:


12345678
23456789

345678910

4567891011

56789101112

Example-2

SYNTAX:- The syntax of nested (for loops) is as follows:

for (initializing ; test condition ; increment / decrement)


{
statements; /* start of outer loop */

d
e
v

for (initializing ; test condition ; increment / decrement)


{

/* start of the inner loop */

o
r
p

body of inner loop;


}

/* end of inner loop */

statements; /* end of the outer loop


}

p
A

#include

Void
{

<stdio.h>

*/

main ( )

int i, j;
clrscr ( );

/* this is a c-build function used to clear screen. It is optional. */

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


{
for (i=1; i<=5; i++)
{
printf (*)
}

printf (\n);
}
}
==================================================================

OUTPUT OF THIS PROGRAM IS

* * * **
* * * **
* * * **
* * * **
EXAMPLE-3: PRINT SERIES FROM 10 TO 1 (10 LENES ARE REPEATED),
USING NESTED LOOPS ( USING FOR AND WHILE LOOPS).
#include
void main ()

o
r
p

{
int a;

p
A

clrscr ();
a=10;

for (k=1;k=10;k++)
{

while (a>=1)
{

printf ("%d",a);
a--;
}
printf("\n");
a= 10;
}

d
e
v

The ouput is as following:


10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1
10 9 8 7 5 4 3 2 1

o
r
p

d
e
v

=====================================================

p
A

Вам также может понравиться