We know that a loop body contains statement or statements. Again a control statement and the body of a loop form a statement. So within a loop body we can put a loop. Loop inside a loop is called Nested loop. For every iteration of the outer loop the inner loop completes it's full course of iteration.
#include < stdio.h>
void main ()
{
int x, y, i, j;
char ch;
clrscr ();
printf ("Enter the character:\n");
scanf ("%c", &ch);
printf ("Enter the number of rows:\n");
scanf ("%d", &x);
printf ("Enter the number of columns:\n");
scanf ("%d", &y);
for (i = 0; i < x; i++) // outer loop
{
for (j = 0; j < y; j++) // inner loop
{
printf ("%c ", &ch);
}
printf ("\n");
}
/* this statement is purely an outer loop statement.*/
}
getch ();
}
The next program will display the following output: - 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 ................. 0 1 2 3 4 5 6 7 8 #include < stdio.h>
void main ()
{
int i, j;
clrscr ();
for (i=0; i < 9; i++)
{
for (j=0; j < i+1; j++)
{
printf ("%d ", j);
}
printf ("\n");
}
getch ();
}
#include " stdio.h"
void main ()
{
int i, j; char ch='*';
clrscr ();
for (i=0;i < 5;i++)
{
for (j=i; j < 5;j++)
{
printf ("%c ", ch);
}
printf ("\n");
}
getch ();
}
1 2 3 4 5 6 7 8 9 10 ............. ............... 79................91 */
#include "stdio.h"
void main()
{
int i,j,x=1;
clrscr();
for(i=0;i <13;i++)
{
for(j=0;j < i+1;j++)
{
printf("%4d",x); x++;
} printf("\n");
}
getch();
}
In our next nested loop program we will display the pattern like: - a a a a a a a a a a ............ .............. a a a a a a a a a
#include "stdio.h"
void main ()
{
int i, j;
clrscr ();
for (i=0;i < 10;i++)
{
for (j=0;j <10;j++)
{
if (j==9-i)
printf ("%c ",'a');
else printf ("%c ",' ');
}
printf ("\n");
}
getch ();
}
To construct a pyramid
#include "stdio.h"
void main()
{
int i,j,k,row,col;
clrscr();
printf("Enter the row and column\t");
scanf("%d%d",&row,&col);
for(i=0;i < row;i++)
{
for(k=i;k< row-1;k++)
printf("%2c",' ');
for(j=0;j < i+1;j++)
{
printf("%4c",'*');
}
printf("\n");
}
getch();
}
Try yourself 1.Write a program to display the following pattern: - 0 1 2 3 1 2 3 2 3 3 2.Write a program to display the following pattern: - 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 We fount that all our outputs are displayed at the left upper corner. How we can display output at other locations? The above program is modified so that the output will be displayed at a location, which are thirty spaces away from the left side.
#include "stdio.h"
void main ()
{
int i, j, s;
clrscr ();
for (i=0;i < 9;i++)
{
for(s=0;s < 30;s++)
printf ("%c",' ');
for (j=0;j < i+1;j++)
{
printf ("%d ", j);
}
printf ("\n");
}
getch ();
}
Using nested loop we can display the following output. 00 01 02 03 04 05 06 a 08 09 10 11 12 13 14 15 16 a 18 19 .......................................... 90 91 92 93 94 95 96 a 98 99 In every 8th column a character 'a' is printed. Also ‘0’ precedes the numbers, which are less than 10.
#include "stdio.h"
void main ()
{
int i, j, x=0;
clrscr ();
for (i=0;i < 10;i++)
{
for (j=1;j < =10;j++)
{
if (j%8==0)
printf ("%c ",'a');
else if(x<10)
printf ("%c%d ",'0',x);
else
printf ("%d ",x);
x++;
}
printf ("\n");
}
getch ();
}
Any Question ? Put Your comments
#include < stdio.h>
void main ()
{
int x, y, i, j;
char ch;
clrscr ();
printf ("Enter the character:\n");
scanf ("%c", &ch);
printf ("Enter the number of rows:\n");
scanf ("%d", &x);
printf ("Enter the number of columns:\n");
scanf ("%d", &y);
for (i = 0; i < x; i++) // outer loop
#include "stdio.h"
void main()
{
int i,j,k,row,col;
clrscr();
printf("Enter the row and column\t");
scanf("%d%d",&row,&col);
for(i=0;i < row;i++)
{
for(k=i;k< row-1;k++)
printf("%2c",' ');
for(j=0;j < i+1;j++)
{
printf("%4c",'*');
}
printf("\n");
}
getch();
}
Try yourself 1.Write a program to display the following pattern: - 0 1 2 3 1 2 3 2 3 3 2.Write a program to display the following pattern: - 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 We fount that all our outputs are displayed at the left upper corner. How we can display output at other locations? The above program is modified so that the output will be displayed at a location, which are thirty spaces away from the left side.
#include "stdio.h"
void main ()
{
int i, j, s;
clrscr ();
for (i=0;i < 9;i++)
{
for(s=0;s < 30;s++)
printf ("%c",' ');
for (j=0;j < i+1;j++)
{
printf ("%d ", j);
}
printf ("\n");
}
getch ();
}
Using nested loop we can display the following output. 00 01 02 03 04 05 06 a 08 09 10 11 12 13 14 15 16 a 18 19 .......................................... 90 91 92 93 94 95 96 a 98 99 In every 8th column a character 'a' is printed. Also ‘0’ precedes the numbers, which are less than 10.
#include "stdio.h"
void main ()
{
int i, j, x=0;
clrscr ();
for (i=0;i < 10;i++)
{
for (j=1;j < =10;j++)
{
if (j%8==0)
printf ("%c ",'a');
else if(x<10)
printf ("%c%d ",'0',x);
else
printf ("%d ",x);
x++;
}
printf ("\n");
}
getch ();
}
how would i get this pattern
ReplyDelete4321
432
43
4
$include
Delete#include
void main()
{ clrscr();
for(int i=1;i<=4;i++)
{ for(int j=4;j>=i;j--)
{ cout<<j;
}
cout<<"\n";
}
getch();
}
#include< stdio.h>
ReplyDeletevoid main()
{
int i,j;
for( i=4; i > 0;i--)
{
for( j=i; j > 0; j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
#include< stdio.h >
ReplyDeletevoid main()
{
int i,j,x;
for( i=0; i < 4;i++)
{
x=4;
for( j=i;j < 4;j++)
{
printf("%d",x);
x--;
}
printf("\n");
}
getch();
}
how to convert a positive DECIMAL Number into BINARY using RECURSION
ReplyDeletePlease check the latest post.
ReplyDeletehow should i get this output
ReplyDeletec
c o
c o m
c o m p
c o m p u
c o m p u t
c o m p u t e
c o m p u t e r
#include< stdio.h>
ReplyDelete#include< string.h>
void main()
{
char str[40];
int i,j,len;
printf("Enter the word:");
scanf("%s",str);
len=strlen(str);
for(i=0;i< len;i++)
{
for(j=0;j< =i;j++)
{
printf("%c",str[j]);
}
printf("\n");
}
getch();
}
1
ReplyDelete23
456
78910
#include< stdio.h>
ReplyDeletevoid main()
{
int i,j,x=1;
for(i=0;i<4;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",x++);
}
printf("\n");
}
getch();
}
how i get this output
ReplyDelete*****
****
***
**
*
#include< stdio.h>
Deletevoid main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
printf("*");
}
printf("\n");
}
}
how i get this output in c language
ReplyDelete1
22
123
4444
12345
666666
1234567
88888888
123456789
101010101010101010
#include< stdio.h>
Deletevoid main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
if(i%2!=0)
printf("%d",j);
else
printf("%d",i);
}
printf("\n");
}
getch();
}
how i get this output in C language
ReplyDelete1 2 3 4 5 6
1 3 5 7 9 11
1 4 7 10 13 16
1 5 7 13 17 21
1 6 11 16 21 26
1 7 13 19 25 31
#include
Deletevoid main()
{
int i,j,x;
clrscr();
for(i=1;i<=6;i++)
{
x=1;
for(j=0;j<6;j++)
{
printf("%d ",x);
x=x+i;
}
printf("\n");
}
getch();
}
how i get
ReplyDelete*
***
*****
#include < stdio.h>
Deletevoid main ()
{
int i,j,x=1;
for(i=0;i<3;i++)
{
for(j=0;j<x;j++)
{
printf("*");
}
x=x+2;
printf("\n");
}
}
how to get
ReplyDelete----1
---12
--123
-1234
12345
" - " denots spaces.
how to get
ReplyDelete----1
---12
--123
-1234
12345
" - " denots spaces.
From today onwards C and C++ programs will be posted in my schooljava.blogspot.com. You will get the codes there in 'Programs using C Language' category.
Delete1
ReplyDelete123
1234
12345
From today onwards C and C++ programs will be posted in my schooljava.blogspot.com. You will get the codes there in 'Programs using C Language' category.
Delete1234
ReplyDelete123
12
1
Posted in schooljava.blogspot.com. From today onwards C and C++ programs will be posted in my schooljava.blogspot.com. You will get the codes there in 'Programs using C Language' category.
DeleteHow do i get this-
ReplyDelete1
123
12345
From today onwards C and C++ programs will be posted in my schooljava.blogspot.com. You will get the codes there in 'Programs using C Language' category.
Deletehow to get this
ReplyDelete*
* *
* *
* * * *
#include
Deletevoid main()
{
int i,j,x;
for(i=0;i<4;i++)
{
if(i==0 || i==3)
x=i;
else
x=1;
for(j=0;j<=x;j++)
printf("*");
printf("\n");
}
getch();
}
#include
ReplyDeletevoid main()
{
int i,j;
for(i=1;i<4;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
getch();
}
how do i will get
ReplyDelete4
43
432
4321
# include< stdio.h>
Deletevoid main()
{
int i,j,x;
for(i=0;i<4;i++)
{
x=4;
for(j=0;j<=i;j++)
{
printf("%d",x);
x++;
}
printf("\n");
}
}
how could i get
Delete4
43
432
4321
pls rply just now its urgent...
#include
Deletevoid main()
{
int i,j,x;
clrscr();
for(i=0;i<4;i++)
x=4;
for(j=0;j<=i;j++)
{
printf("%d",x--);
}
printf("\n");
}
getch();
}