problem in nested loop (C programming)

how can i make a code that(the output) appear like these(in nested looping section):

1
2
3
4
5
6
Please enter an integer>5
     1     
    121
   12321
  1234321
 123454321


Have an outer loop which does each row.

In that loop have 2 more loops. One which counts up to the row number, and another which counts down from it. Each printing numbers as they go.
can you show me the code..just now i have programming class in my university..i sat on the back..so i didnt notice anything about this nested loop section..and plus im blurred..

so can you help me for this output..i mean..can you help me with the code..maybe with that i can learn it
can you show me the code


No. Not unless you show me you are making an effort to do it yourself. This isn't a "do my homework for me" site.
im sorry..ok...i have done a bit..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <conio.h>

int main (void)

{
    int num, row, col, i;
    printf("Please enter a number:");
    scanf("%d", &num);
    
    for(row=0; row<num; row++){
               
            for(col=1; col<(num*2-1); col++){
                    printf(" ");
                    if (col==4)
                    printf("%d", num);
            }
    printf("\n");
    }
    
 
 
 
    
getch();
return 0;
}


in that code what is the problems..
Alright. =)
I don't mind helping people figure stuff out, but I don't like to hand out solutions, either. "Teach a man to fish", etc, etc.

You have the nested loop idea right, but it would probably help to split the job up into several parts. This is how I would approach the problem:

1
2
3
4
5
6
7
8
9
10
for(row = 1; row <= num; ++row)
{
  // have a loop here which draws the appropriate number of spaces

  // have another loop here which prints 1 up to the current row number (row)

  // have a 3rd loop here which prints numbers counting down from 'row'

  printf("\n");
}
Thx!!..i will try this ....thank you very much..ill will inform u aftr i get the output...thx very much!
I GOT IT!!!thx2!!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <conio.h>
#include <stdio.h>

int main ()
{
    int i , j ,num, k,l ;
    
    
    printf ("Enter an integer > ");
    scanf ("%d",&num);
    
    for (i=1; i<=num; i++)
        {
        
              for (k=num; k>i; k--)
                  {
                      printf (" ");
                      
                  }
              for (j=1; j<=i; j++)
                  {
                      printf ("%d",j);
                  }
              for (l=j-2; l!=0; l--)
                  {
                      printf ("%d",l);
                  }
                 
              printf("\n");
        }
    
    getch();
    return (0);
}



Last edited on
and one more..how can there'll be these kind of output??

1
2
3
4
5
6
enter an integer >5
    1
   2 2
  3   3
 4     4
555555555


how can there'll be an empty spaces between the number..can you help me..
Last edited on
Congrats @ your solution. =)

This new problem is more or less the same idea. Take it one row at a time. Try to look at the desired output and construct a logic flow for yourself.

For this one... it might help to treat the top and bottom rows seperately from the inner rows:

1
2
3
4
5
6
7
8
// print top row

for( ... )
{
  // print inner rows
}

// print bottom row 


Notice that all the inner rows all follow a pattern. They all start with some spaces, print a number, have some more spaces, then print that number again. Figure out the pattern, then it should be easy to translate it into code.
Topic archived. No new replies allowed.