CS50 Problem Set 1
Here’s my answer for the CS50 Problem Set 1. Hope that will help you a bit.
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("#"); }
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; }
while (cash > 9) { cash -= 10; n_10 += 1; }
while (cash > 4) { cash -= 5; n_5 += 1; }
while (cash > 0) { cash -= 1; n_1 += 1; } sum = n_25 + n_10 + n_5 + n_1; printf("%d\n", sum); return 0; }
|