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

34 comments:

  1. Replies
    1. $include
      #include
      void main()
      { clrscr();
      for(int i=1;i<=4;i++)
      { for(int j=4;j>=i;j--)
      { cout<<j;
      }
      cout<<"\n";
      }
      getch();

      }

      Delete
  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
  8. how i get this output

    *****
    ****
    ***
    **
    *

    ReplyDelete
    Replies
    1. #include< stdio.h>
      void main()
      {
      int i,j;
      for(i=0;i<5;i++)
      {
      for(j=i;j<5;j++)
      {
      printf("*");
      }
      printf("\n");
      }
      }

      Delete
  9. how i get this output in c language
    1
    22
    123
    4444
    12345
    666666
    1234567
    88888888
    123456789
    101010101010101010

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

      Delete
  10. how i get this output in C language
    1 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

    ReplyDelete
    Replies
    1. #include

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

      Delete
  11. Replies
    1. #include < stdio.h>
      void main ()
      {
      int i,j,x=1;
      for(i=0;i<3;i++)
      {
      for(j=0;j<x;j++)
      {
      printf("*");
      }
      x=x+2;
      printf("\n");
      }
      }

      Delete
  12. how to get

    ----1
    ---12
    --123
    -1234
    12345
    " - " denots spaces.

    ReplyDelete
  13. how to get

    ----1
    ---12
    --123
    -1234
    12345
    " - " denots spaces.

    ReplyDelete
    Replies
    1. 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.

      Delete
  14. Replies
    1. 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.

      Delete
  15. Replies
    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.

      Delete
  16. Replies
    1. 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.

      Delete
  17. Replies
    1. #include
      void 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();
      }

      Delete
  18. #include
    void main()
    {
    int i,j;
    for(i=1;i<4;i++)
    {
    for(j=1;j<=i;j++)
    printf("%d",i);
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  19. Replies
    1. # include< stdio.h>
      void 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");
      }
      }

      Delete
    2. how could i get
      4
      43
      432
      4321
      pls rply just now its urgent...

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

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner