Home > Software design >  C program to print the power of 2 Triangle pattern using nested loops
C program to print the power of 2 Triangle pattern using nested loops

Time:09-08

enter image description here

I am trying this code;

#include <stdio.h>

void main()
{
    int no_row=5,c=1,blk,i,j;
    //printf("Input number of rows: ");
    //scanf("%d",&no_row);
    for(i=0;i<no_row;i =2)
    {
        for(blk=1;blk<=no_row-i;blk  )
            printf("  ");
        for(j=0;j<=i;j  )
        {
            if (j==0||i==0)
                c=1;
            else
                c=c*(i-j 1)/j;
            printf("% 4d",c);
        }
        printf("\n");
    }
}

I need a help to write a code to get a output like in picture. power 2 pattern is in the picture

CodePudding user response:

There's the hard way (using 5 different variables), and there's the easy way (using a buffer and three variables).

This has "nested loops" and counts up and down, and indents appropriately.

#include <stdio.h>

int main() {
    char buf[ 128 ], *at;
    int r, i;

    int lftBlnk = 32;
    for( r = 1; r <= 256; r *= 2 ) {
        at = buf;
        for( i = 1; i <= r; i *= 2 ) // ascending
            at  = sprintf( at, "%-4d", i );
        for( i /= 4; i; i /= 2 ) // descending
            at  = sprintf( at, "%-4d", i );

        printf( "%*s%s\n", lftBlnk, "", buf ); // indent & output
        lftBlnk -= 4;
    }

    return 0;
}

Output

                                1
                            1   2   1
                        1   2   4   2   1
                    1   2   4   8   4   2   1
                1   2   4   8   16  8   4   2   1
            1   2   4   8   16  32  16  8   4   2   1
        1   2   4   8   16  32  64  32  16  8   4   2   1
    1   2   4   8   16  32  64  128 64  32  16  8   4   2   1
1   2   4   8   16  32  64  128 256 128 64  32  16  8   4   2   1

EDIT:

Having some fun, stripping out what's unnecessary, the code reduces to be quite compact.

int main() {
    for( int lftBlnk = 32, r = 1; r <= 256; r *= 2, lftBlnk -= 4 ) {
        printf( "%*s", lftBlnk, "");
        int i;
        for( i = 1; i <= r; i *= 2 ) printf( "%-4d", i );
        for( i /= 4; i; i /= 2 ) printf( "%-4d", i );
        putchar( '\n' );
    }

    return 0;
}

In fact, let's get rid of a variable, add another, and improve the flexibility of the code (without repeated constants like '4' and magic numbers like 256.) Now, specify the no. of rows, and allow enough width for each column...

int main() {
    int rows = 11, wid = 5;
    for( int r = 0; r < rows; r   ) {
        printf( "%*s", (rows-1-r)*wid, "");
        int i;
        for( i = 1; i <= (1 << r); i *= 2 ) printf( "%-*d", wid, i );
        for( i /= 4; i; i /= 2 ) printf( "%-*d", wid, i );
        putchar( '\n' );
    }

    return 0;
}

                                                  1
                                             1    2    1
                                        1    2    4    2    1
                                   1    2    4    8    4    2    1
                              1    2    4    8    16   8    4    2    1
                         1    2    4    8    16   32   16   8    4    2    1
                    1    2    4    8    16   32   64   32   16   8    4    2    1
               1    2    4    8    16   32   64   128  64   32   16   8    4    2    1
          1    2    4    8    16   32   64   128  256  128  64   32   16   8    4    2    1
     1    2    4    8    16   32   64   128  256  512  256  128  64   32   16   8    4    2    1
1    2    4    8    16   32   64   128  256  512  1024 512  256  128  64   32   16   8    4    2    1

CodePudding user response:

First, fix the compute to get power of 2 Then use tab as separator to get the same alignment as on your picture

                                    1   
                                1   2   1   
                            1   2   4   2   1   
                        1   2   4   8   4   2   1   
                    1   2   4   8   16  8   4   2   1   
                1   2   4   8   16  32  16  8   4   2   1   
            1   2   4   8   16  32  64  32  16  8   4   2   1   
        1   2   4   8   16  32  64  128 64  32  16  8   4   2   1   
    1   2   4   8   16  32  64  128 256 128 64  32  16  8   4   2   1   
1   2   4   8   16  32  64  128 256 512 256 128 64  32  16  8   4   2   1   
#include <stdio.h>

void main()
{
    int no_row=10,c=1,blk,i,j;
    //printf("Input number of rows: ");
    //scanf("%d",&no_row);
    for(i=0;i<no_row;i  )
    {
        for(blk = 0; blk < no_row - i - 1; blk  )
            printf("\t");
        c = 1;
        printf("%d\t",c);
        for(j = 0;j < i;j  )
        {
            c=c*2;
            printf("%d\t",c);
        }
        for(j = 0;j < i;j  )
        {
            c=c/2;
            printf("%d\t",c);
        }
        printf("\n");
    }
}

CodePudding user response:

I used another method to achieve what you said

This method requires two parameters, one is the multiple that needs to be advanced, and the other is the number of lines that need to be advanced.

#include <stdio.h>

//row main how many lines do you want to print
//multiple means you want it to be several times expanded
void  Triangle(int multiple, int row){
    int value = 1, mid;
    for (int i = 0; i < row; i  )
    {
        for (int j = 0; j < 2*row-1; j  )
        {
            mid = (2*(row-1) 1)/2;  //It means that the middlemost pointer points to
            //mid - i adn mid   i means that there is a majority of the number that needs to be displayed on this line
            if (j >= mid -i && j<= mid  i)
            {
                if (j>mid)
                {
                    value = value / multiple;
                    printf("%d", value);
                }else{
                    printf("%d", value);
                    if (j!= mid)
                    {
                        value = value * multiple;
                    }
                }
            }else{
                printf(" ");
            }
            printf("    ");
        }
        value = 1;
        printf("\n");
    }
} 

//Example
int main(){
    Triangle(2,7);
    return 0;
}

result:

enter image description here

Of course you can even use multiples of 3 or more

int main(){
    Triangle(3,7);
    return 0;
}

result:

enter image description here

  • Related