C language: draw a pyramid from Mario


An interesting puzzle for beginners from CS50 (Harvard).

Task: draw a pyramid from the Mario (mario-less.c) game using # symbols (lattice or hash). The user is asked: “How many blocks to make a pyramid from?”, he enters a number and the pyramid is drawn, right-aligned, like this:

       #
      ##
     ###
    ####
   #####
  ######
 #######

igroglaz solution:

#include <stdio.h>

int main(void)
{

    int height = 0;
    int newline;
    int hash;
    int dot;

    printf("Enter height of pyramid. ");

    do
    {
        printf("It must be a positive integer:\n");
        scanf("%d", &height);
        while (getchar() != '\n') ; // clear buffer (in case of 'a')
    }
    while (height <= 0);

    for (newline = 1; newline <= height; newline++)
    {
        for (dot = 0; height - newline > dot; dot++)
            printf(" ");

        for (hash = 1; hash <= newline; hash++)
            printf("#");

        printf("\n");
    }

    getchar();
    getchar();
    return 0;
}

Hello everyone, this is another solution (Stukensia):

#include <cs50.h>
#include <stdio.h>

int main(void)

{
    int n;
    do
    {
        n = get_int("Type number from 1 to 8\n");
    }
    while (n < 1 || n > 8);
    printf("Your number is:%i\n",n);
    int i;
    int r;
    int p;
    for (i=0; i<n; i++)
    {
        for (p=0; p<=(n-i); p++)
            {
                printf(" ");

            }
        for (r=0; r<=i; r++)
        {
            printf("#");
        }

    printf("\n");
    }

}

This is what the program looks like:

The second, more difficult (in the opinion of the creators of CS50) option is to make the pyramid mirror (mario-more.c):

       #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########

It is already much easier to make it: by rearranging the order of the code from the first pyramid:

#include <stdio.h>

int main(void)
{

    int height = 0;
    int newline;
    int hash;
    int dot;

    printf("Enter height of pyramid. ");

    do
    {
        printf("It must be a positive integer:\n");
        scanf("%d", &height);
        while (getchar() != '\n') ; // clear buffer (in case of 'a')
    }
    while (height <= 0);

    for (newline = 1; newline <= height; newline++)
    {
        for (dot = 0; height - newline > dot; dot++)
            printf(" ");

        for (hash = 1; hash <= newline; hash++)
            printf("#");

        printf("  ");

        for (hash = 1; hash <= newline; hash++)
            printf("#");

        printf("\n");
    }

    getchar();
    getchar();
    return 0;
}

Another solution to the second more difficult task “mirror pyramid” (Shtukentsia):

#include <cs50.h>
#include <stdio.h>

int main(void)

{
    int n;
    do
    {
        n = get_int("Type number from 1 to 8\n");
    }
    while (n < 1 || n > 8);
    printf("Your number is: %i\n",n);
    int i;
    int r;
    int p;
    for (i=0; i<n; i++)
    {
        for (p=0; p<=(n-i); p++)
        {
            printf(" ");
        }
        for (r=0; r<=i; r++)
        {
            printf("#");
        }
        printf("  ");
        for (r=0; r<=i; r++)
        {
            printf("#");
        }

    printf("\n");
    }

}

This is what the program looks like:

After checking bugs I updated my code of simple pyramid:

#include <cs50.h>
#include <stdio.h>

// Draw pyramid with hashtags

int main(void)
{
    int n; // how big is pyramid

    do
    {
        n = get_int("Type number from 1 to 8\n"); // get number of # from 1 to 8
    }
    while (n < 1 || n > 8);

// Drawing pyramid

    int i;
    int r;
    int p;
    for (i = 0; i < n; i++)
    {
        for (p = 1; p < (n - i); p++)
        {
            printf(" ");
        }

        for (r = 0; r <= i; r++)
        {
            printf("#");
        }
        printf("\n");
    }
}

And my new code of double pyramid:

#include <cs50.h>
#include <stdio.h>

// Draw double pyramid with hashtags

int main(void)
{
    int n;
    do
    {
        n = get_int("Type number from 1 to 8\n"); // Ask user to type the size 1-8
    }
    while (n < 1 || n > 8);

// Start to draw the pyramid

    int i;
    int r;
    int p;
    for (i = 0; i < n; i++)
    {
        for (p = 1; p < (n - i); p++)
        {
            printf(" ");
        }
        for (r = 0; r <= i; r++)
        {
            printf("#");
        }
        printf("  "); // prints the middle of pyramid
        for (r = 0; r <= i; r++)
        {
            printf("#");
        }
        printf("\n");
    }
}

This entry was posted in C language (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.