Prachi Rajesh Jansari
Prachi Rajesh Jansari

Reputation: 25

Write a basic half pyramid pattern program

Have tried few basic pattern

trying to get pattern

1
2 4
3 6 12
4 8 16 32

SO far trying to find the proper sequence, my idea is that need another variable lets say num, and need to create a sequence for num to print num eventually

#include <stdio.h>
int main()
{
    int rows = 0  , i, j , num,num2;


   do{
        printf("please enter the number of rows: ");
       scanf("%d",&rows);
    }while(rows <=2 );
    printf("printing a half pyramid of %d rows", rows);
    printf("\n");
     for( i = 1; i <=rows; ++i) {
        for (j = 1; j <= i; ++j ) {

            printf("%d ",  );


        }
       printf("\n");

    }
    return 0;
}

Not being able to figure out a sequence

Upvotes: 1

Views: 985

Answers (3)

bvsh
bvsh

Reputation: 64

Tricky Pattern. Here, is a logic for that pattern with implementation. 'n' is the number of rows.

#include <stdio.h>
int main(void) {
int n = 4;
    for(int i=1; i<=n; i++) {
        int k=i;
        printf("%d%s",k," ");
        for(int j=1; j<i; j++) {
            k = k*2;
            printf("%d%s",k," ");
        }
        printf("\n");
    }
    return 0;
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

We, beginners, should help each other.:)

Here you are.

#include <stdio.h>

int main(void) 
{
    while ( 1 )
    {
        const unsigned int Base = 10;

        printf( "Enter the height of a pyramid (0 - exit): " );

        unsigned int n;

        if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;

        int width = 0;
        unsigned int tmp = n * n;

        do { ++width; } while ( tmp /= Base );

        putchar( '\n' );

        for ( unsigned int i = 0; i < n; i++ )
        {
            unsigned int value = i + 1;
            for ( unsigned int j = 0; j++ <= i; )
            {
                printf( "%*u ", width, value * j );
            }
            putchar( '\n' );
        }

        putchar( '\n' );
    }

    return 0;
}

The program output might look like

Enter the height of a pyramid (0 - exit): 1

1 

Enter the height of a pyramid (0 - exit): 2

1 
2 4 

Enter the height of a pyramid (0 - exit): 3

1 
2 4 
3 6 9 

Enter the height of a pyramid (0 - exit): 4

 1 
 2  4 
 3  6  9 
 4  8 12 16 

Enter the height of a pyramid (0 - exit): 5

 1 
 2  4 
 3  6  9 
 4  8 12 16 
 5 10 15 20 25 

Enter the height of a pyramid (0 - exit): 6

 1 
 2  4 
 3  6  9 
 4  8 12 16 
 5 10 15 20 25 
 6 12 18 24 30 36 

Enter the height of a pyramid (0 - exit): 7

 1 
 2  4 
 3  6  9 
 4  8 12 16 
 5 10 15 20 25 
 6 12 18 24 30 36 
 7 14 21 28 35 42 49 

Enter the height of a pyramid (0 - exit): 8

 1 
 2  4 
 3  6  9 
 4  8 12 16 
 5 10 15 20 25 
 6 12 18 24 30 36 
 7 14 21 28 35 42 49 
 8 16 24 32 40 48 56 64 

Enter the height of a pyramid (0 - exit): 9

 1 
 2  4 
 3  6  9 
 4  8 12 16 
 5 10 15 20 25 
 6 12 18 24 30 36 
 7 14 21 28 35 42 49 
 8 16 24 32 40 48 56 64 
 9 18 27 36 45 54 63 72 81 

Enter the height of a pyramid (0 - exit): 0

The loops in the program can also look like

for ( unsigned int i = 0; i++ < n;  )
{
    unsigned int value = i;
    for ( unsigned int j = 0; j < i; j++ )
    {
        printf( "%*u ", width, value );
        value += i;
    }
    putchar( '\n' );
}

or without introducing the intermediate variable value like

for ( unsigned int i = 0; i < n; i++ )
{
    for ( unsigned int j = 0; j++ <= i; )
    {
        printf( "%*u ", width, j * ( i + 1 ) );
    }
    putchar( '\n' );
}

You yourself can add a check to the program that n * n is not greater than UINT_MAX.

Edit: As you changed the displayed values in the pattern then the program can look for example the following way

#include <stdio.h>
#include <math.h>

int main(void) 
{
    while ( 1 )
    {
        const unsigned int Base = 10;

        printf( "Enter the height of a pyramid (0 - exit): " );

        unsigned int n;

        if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;

        int width = 0;
        unsigned long long int tmp = n * ( long long unsigned )pow( 2, ( n - 1 ) );

        do { ++width; } while ( tmp /= Base );

        putchar( '\n' );

        for ( unsigned int i = 0; i++ < n;  )
        {
            unsigned int value = i;
            for ( unsigned int j = 0; j < i; j++ )
            {
                printf( "%*u ", width, value );
                value *= 2;
            }
            putchar( '\n' );
        }

        putchar( '\n' );
    }

    return 0;
}

The program output might look like

Enter the height of a pyramid (0 - exit): 10

   1 
   2    4 
   3    6   12 
   4    8   16   32 
   5   10   20   40   80 
   6   12   24   48   96  192 
   7   14   28   56  112  224  448 
   8   16   32   64  128  256  512 1024 
   9   18   36   72  144  288  576 1152 2304 
  10   20   40   80  160  320  640 1280 2560 5120 

Enter the height of a pyramid (0 - exit): 0

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

The code you were given literally contains all the parts necessary. All that remains for you is to fill out this line inside the nested loop:

printf("%d ", ‹what goes here?›);

To find the answer you need to find how the value relates to the current row and column (give by i and j, respectively).

You don’t need an additional variable num (to be clear, you can create one, but it’s not necessary to solve this problem).

Upvotes: 2

Related Questions