Search This Blog

Loading...

Saturday, March 27, 2010

C programs on pattern printing using nested loop

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

10 comments:

  1. what is the program of the following output using nested for loop.
    4 3 2 1
    3 2 1
    2 1
    1

    ReplyDelete
  2. #include< stdio.h>
    void main()
    {
    int i,j;
    for( i=4; i > 0;i--)
    {
    for( j=i; j > 0; j--)
    {
    printf("%d",j);
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  3. #include< stdio.h >
    void 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();
    }

    ReplyDelete
  4. how to convert a positive DECIMAL Number into BINARY using RECURSION

    ReplyDelete
  5. how should i get this output
    c
    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

    ReplyDelete
  6. #include< stdio.h>
    #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();
    }

    ReplyDelete
  7. #include< stdio.h>
    void main()
    {
    int i,j,x=1;
    for(i=0;i<4;i++)
    {
    for(j=0;j<=i;j++)
    {
    printf("%d",x++);
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete