CS50 Problem Set 1

CS50 Problem Set 1

King Hua Lv2

CS50 Problem Set 1

Here’s my answer for the CS50 Problem Set 1. Hope that will help you a bit.

Problem 1: Hello, It’s Me

1
2
3
4
5
6
7
8
9
#include <cs50.h>
#include <stdio.h>

int main(void)
{
string name = get_string("What's your name?\n");
printf("Hello, %s\n", name);
return 0;
}

Problem 2: Mario

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cs50.h>
#include <stdio.h>

void mario(int n);

int main(void)
{
int n;
do
{
n = get_int("Height: ");
}
while (n < 1 || n > 8);
mario(n);
return 0;
}

void mario(int n)
{
for (int i = 1; i < n + 1; i++)
{
for (int j = 0; j < n - i; j++)
{
printf(" ");
}

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

printf(" ");

for (int k = 0; k < i; k++)
{
printf("#");
}
// for (int j = 0; j < n - i; j++)
// {
// printf(" ");
// }

printf("\n");
}
}

Problem 3: Cash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cs50.h>
#include <stdio.h>

int main(void)
{
int cash;
do
{
cash = get_int("Change owed: ");
}
while (cash < 0);

int n_25 = 0;
int n_10 = 0;
int n_5 = 0;
int n_1 = 0;
int sum = 0;

while (cash > 24)
{
cash -= 25;
n_25 += 1;
}
// printf("%d\n", n_25);

while (cash > 9)
{
cash -= 10;
n_10 += 1;
}
// printf("%d\n", n_10);

while (cash > 4)
{
cash -= 5;
n_5 += 1;
}
// printf("%d\n", n_5);

while (cash > 0)
{
cash -= 1;
n_1 += 1;
}
// printf("%d\n", n_1);
sum = n_25 + n_10 + n_5 + n_1;
printf("%d\n", sum);
return 0;
}
  • Title: CS50 Problem Set 1
  • Author: King Hua
  • Created at : 2026-02-10 13:48:42
  • Updated at : 2026-02-16 21:01:56
  • Link: https://kinghua0629.github.io/2026/02/10/CS50-Problem-Set-1/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments