c programs on pattern printing using nested loop

7
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");

Upload: ncetcsedept

Post on 22-Apr-2015

185 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programs on Pattern Printing Using Nested Loop

C programs on pattern printing using nested loopWe 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++)  

Page 2: C Programs on Pattern Printing Using Nested Loop

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

Page 3: C Programs on Pattern Printing Using Nested Loop

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 (){

Page 4: C Programs on Pattern Printing Using Nested Loop

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);elseprintf ("%d ",x);x++;}printf ("\n");}getch ();}

Any Question ? Put Your commentsPosted by Dhananjoy Chakraborty at 3:28 PM

8 comments:

5b8cb90c-76db-11e0-8390-000bcdcb8a73 said...how would i get this pattern 4321432434

May 5, 2011 11:19 AM

Anonymous said...what is the program of the following output using nested for loop.4 3 2 13 2 12 11

August 10, 2011 1:11 PM

Dhananjoy Chakraborty said...#include< stdio.h>void main(){int i,j;for( i=4; i > 0;i--){for( j=i; j > 0; j--)

Page 5: C Programs on Pattern Printing Using Nested Loop

{printf("%d",j);}printf("\n");}getch();}

August 12, 2011 7:32 AM

Dhananjoy Chakraborty said...#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();}

August 12, 2011 7:53 AM

Anonymous said...how to convert a positive DECIMAL Number into BINARY using RECURSION

August 22, 2011 5:45 PM

Dhananjoy Chakraborty said...Please check the latest post.

August 24, 2011 5:44 PM

snigdha said...how should i get this outputcc oc o mc o m p

Page 6: C Programs on Pattern Printing Using Nested Loop

c o m p uc o m p u tc o m p u t ec o m p u t e r

November 27, 2011 11:40 PM

Dhananjoy Chakraborty said...#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();}

November 29, 2011 6:55 AM

Post a Comment

Links to this postCreate a Link

Newer Post Older Post Home

Subscribe to: Post Comments (Atom)POPULAR POSTS

Array of structures