C program to print the power of 2 Triangle pattern using nested loops

Viewed 82

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

4 Answers

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

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

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

Below is my solution based on your code:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define MAX 64

int main(int argc, char **argv)
{
    while (--argc, *++argv) {

        int N = atoi(argv[0]);
        if (N <= 0 || N >= 64)
            continue;

        int wide = snprintf(NULL, 0,
            "%llu", 1ULL << (N-1)) + 1;

        for(int i = 0; i < N; i++) {

            /* print spaces */
            for(int j = 1; j < N - i; j++)
                printf("%*s", wide, "");

            uint64_t bit, end = 1ULL << i;

            /* print the growing seq */
            for(bit = 1; bit && bit < end; bit <<= 1)
                printf("%*lu", wide, bit);

            /* print the decreasing seq */
            for(; bit; bit >>= 1)
                printf("%*lu", wide, bit);
            printf("\n");
        }
    }
    return 0;
}

It first calculates the width of the biggest number to be printed and uses that as the field length to print the left spaces, and the gap between numbers. The program takes, as command line parameters the heights of the triangles to be printed and uses uint64_t to be able to reach sizes upto 63.

The code uses bit operators to calculate the powers of 2, and bit shifts to produce the successive powers of two.

I produced a right justified output, at it seemed to me more natural, but in order to produce left shifted output, you have only to add a - between the '%' and the '*' chars, in the output to left justify it.

Below is a sample run

$ a.out 3 11
     1
   1 2 1
 1 2 4 2 1
                                                      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
$ _
Related