From b1f8474ed12fc0127297615c83b8d170a6155a43 Mon Sep 17 00:00:00 2001 From: Parul-Seth <42246607+Parul-Seth@users.noreply.github.com> Date: Tue, 18 Dec 2018 00:22:45 +0530 Subject: [PATCH] add text (#31948) nested for loop --- guide/english/c/for/index.md | 64 +++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/guide/english/c/for/index.md b/guide/english/c/for/index.md index 4edac67c75..f147587d33 100644 --- a/guide/english/c/for/index.md +++ b/guide/english/c/for/index.md @@ -111,35 +111,23 @@ Output: ``` ## Example for printing star pattern for pyramid -```c - +```C #include -int -main () -{ +int main() { int i, j; - for (i = 1; i <= 5; i++) - { - - for (j = i; j < 5; j++) - { - printf (" "); - } - - - for (j = 1; j <= (2 * i - 1); j++) - { - printf ("*"); - } - - - printf ("\n"); + for (i = 1; i <= 5; i++) { + for (j = i; j < 5; j++) { + printf (" "); } + for (j = 1; j <= (2 * i - 1); j++) { + printf ("*"); + } + printf ("\n"); + } return 0; } ``` - Output: ```shell * @@ -148,6 +136,34 @@ Output: ******* ********* ``` + +## Nested `for` loop in C +```C +#include +#include + +int main() +{ + int i, k, levels, space; + printf("Enter the number of levels in pyramid:"); + scanf("%d",&levels); + + space = levels; + + for ( i = 1 ; i <= levels ; i++ ) { + for ( k = 1 ; k < space ; k++ ) { + printf(" "); + } + space--; + + for ( k = 1 ; k <= 2*i - 1 ; k++ ) { + printf("*"); + } + printf("\n"); + } + return 0; +} +``` ## Syntax of For Infinite loop @@ -200,7 +216,6 @@ int main () { int array[] = {1, 2, 3, 4, 5}; int i; // You declare the variable before the for loop -<<<<<<< HEAD for (i = 0; i < 5; i++) { // Now you won't have a problem printf("Item on index %d is %d\n", i, array[i]); } @@ -255,6 +270,3 @@ output: 4 5 ``` -======= -`` ->>>>>>> I have added a 'Note' part