CS50 Psets 1 mario Problems (2022)

The solution to CS50 Psets 1 mario Problems (2022)

CS50 psets 1 Mario -less

CS50 psets 1 Mario

In this problem cs50 psets 1 Mario -less, we have to recreate the right-aligned pyramid that we saw at the end of World 1-1 in Nintendo’s Super Mario Brothers using C language. We have to do this using the (#) symbol.

We have to write a program that allows the user to decide just how tall the pyramid should be by prompting them for a positive integer value for the height of the pyramid between 1 and 8.

You may also like to read: The solution to CS50 Psets 2 Caesar Problem (2022)

So the output should be like this,

$ ./mario
Height: 8
       #
      ##
     ###
    ####
   #####
  ######
 #######
########

$ ./mario
Height: 4
   #
  ##
 ###
####

$ ./mario
Height: 2
 #
##

$ ./mario
Height: 1
#

Also if the user does not input a positive integer value between 1 and 8 the program should re-prompt the user until they input the correct values.

$ ./mario
Height: -1
Height: 0
Height: 42
Height: 50
Height: 4
   #
  ##
 ###
####

This is the solution that I created for this problem,

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

int main(void)
{
    int h;

    do
    {
        h = get_int("Height: ");
    }
    while (h < 1 || h > 8);

    for (int i = 1; i <= h; i++){
            for (int k = i; k < h; k++)
            {
                printf(" ");
            }
            for (int j=1; j<=i; j++)
            {
                printf("#");
            }
            printf("\n");
    }
}

Mario-more

CS50 psets 1 Mario

In this problem, we have to create the below structure using the c language. So the problem should prompt the user to get the height as in the above problem. and then output an adjacent pyramid using (#) symbols

So here is my solution for this problem,

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

int main(void)
{
    int h;

    do {
        h = get_int("Height: ");
    }
    while (h < 1 || h > 8);

    for (int i = 1; i <= h; i++){
            for (int k = i; k < h; k++)
            {
                printf(" ");
            }
            for (int j=1; j<=i; j++)
            {
                printf("#");
            }
            printf("  ");
            for (int l=1; l<=i; l++)
            {
                printf("#");
            }
            printf("\n");
    }
}

My website: dasunsucharith.me

(Visited 11 times, 1 visits today)

Leave a Reply