Skip to content

CS50 Week 1 C — Hello World, Mario Pyramids and Credit Card Validation

Terminal output showing three CS50 Week 1 C programs — Hello World greeting, a double Mario pyramid of hash symbols, and a credit card validation result — on a dark code editor background.

Week 1 of CS50x is where things get real. You go from watching lectures about how computers think to actually writing C — a language that does not hold your hand, does not guess what you meant, and will absolutely refuse to compile if you forget a semicolon. The three problem sets for this week are Hello, Mario, and Credit, and together they cover a solid range of what C programming is actually about. Hello is the warm-up: prompt the user for their name, store it in a string, print a greeting. Simple on the surface, but it introduces the CS50 library, the get_string function, and the idea that in C you are always thinking about types and memory, even when you are just saying hello.

Mario steps things up quite a bit. The task is to print a double pyramid of hashes — the kind you see in the classic Super Mario Bros game — using nothing but loops and printf. The tricky part is the spacing. Getting the right number of spaces on the left, then the hashes, then the two-space gap in the middle, then the hashes again on the right, all scaling correctly with the height the user picks — that takes a bit of working out. The approach here uses three nested loops per row: one to handle the leading spaces, one for the left column of hashes, and one for the right. Input validation is handled with a do-while loop that keeps prompting until the user gives a number between 1 and 8, which is a clean and efficient pattern. Credit is the most involved of the three. It takes a credit card number as a long, checks the digit count, runs Luhn’s algorithm to verify the checksum, and then identifies whether the card is a Visa, Mastercard, or American Express based on the leading digits. The code is broken into four separate functions — digit counting, digit extraction, Luhn’s algorithm, and card type identification — which keeps things readable and makes each piece easy to test independently.

What Week 1 really teaches you is how to think in C. You cannot be vague. Every variable has a type, every loop has a condition, every function has a return type. Writing the credit card validator was a good lesson in that — working with a long across multiple functions, extracting individual digits using powers of ten, and making sure the Luhn sum logic handles both single and double-digit results correctly. These are the kinds of problems that build real programming instincts. By the end of the week the basics of C feel much less intimidating, and the advantage of starting with a lower-level language like this is that everything you learn later — Python, Flask, SQL — feels more intuitive because you understand what is happening underneath.

The full code for this week is up on GitHub — you can browse the Week 1 folder directly to see all three programs, or explore the entire CS50x repository to follow the course week by week as it progresses.

claude dev

Developer and designer based in the UK, building digital experiences.

Leave a Comment