CS50 PSet 1: Hello Solution

By Suraj Chaudhary

When you start taking CS50, Hello, Mario (less and more), cash, and/or credit are some of the first questions that you will have to submit. In this article, I am going to share with you how you can solve the Hello problem. Here’s how to solve the hello problem in CS50.

The first thing to do is to understand what we are trying to build. You can read the instructions from CS50’s website. Our goal, simply, is to create a program that asks the user for input and says hello to the user. This could look like this:

$ ./hello
What's your name? Suraj
Hello, Suraj!
$

If you want to learn how to earn a verified CS50 certificate for free, read CS50: How To Study At Harvard For FREE (and get a free certificate).

To solve this problem, we have to do two steps. First, prompt the user for input and store it in a variable. Then to use that variable to say hello to the user. So how would you solve this? Here’s how:

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

int main(void)
{
    // Ask for the name
    string name = get_string("What's your name? ");

    // Say hello to the user
    printf("Hello, %s!\n", name);
}

Explanation

First, we write the basic structure for any c program, i.e. including the header files and declaring the main function. After we have the basic structure, we can start building the program inside the main function.

We first ask the user to enter his/her name using get_sting() and we store it in the string name. Keep in mind that to use the data type string, you must include cs50’s header file, such as #include <cs50.h>. We might code the first part of prompting the user for his/her name as:

string name = get_string("What's your name? ");

Now, we have the user’s name and we have stored it in the string name. Our next task is to say hello to the user. How may we do so? We first use a printf() function to allow us to print anything on the screen. Then, we pass in two inputs to the function: first “Hello, ” and the second the name of the user. Here’s how we can do that.

printf("Hello, %s!\n", name);

It might appear cryptic but the code is fairly simple. First, we passed “Hello, ” as input. Then we added a placeholder for string, i.e. %s, to tell the computer that we are going to print a string. Then we added a new line so that the code doesn’t look messy. Remember though that the new line will appear after the user’s name. Why? Because the \n is entered after the placeholder for the user’s name, i.e. %s. After we have provided the first input to say “Hello, “, we will pass in the second input, which is the user’s name. To do so, we use another comma (,) after the double quotes, and enter the variable’s name, i.e. name. At last, we end the line with a semi-colon (;).

After we have done this much, we will have a program that says hello to the user. It would appear in the terminal as

$ ./hello
What's your name? Suraj
Hello, Suraj!
$

If you have any confusion or problem, feel free to drop a comment down below. In the meantime, you can read these articles.

Also Read

  1. CS50: How To Study At Harvard For FREE (and get a free certificate)
  2. How To Get A Domain Name For FREE in 2022
  3. How To Stop Ads On YouTube and Other Websites
  4. How To Use Microsoft Office Apps For FREE (even offline)
  5. How To Read Wall Street Journal, NY Times, etc For FREE
  6. Best Websites To Watch Movies Online For FREE
  7. How to use Spotify for FREE

Suraj Chaudhary is the founder of HighApproach. He is also a content creator, web developer, SEO expert, entrepreneur, blogger, writer, and a tech-enthusiast. On this blog, he shares helpful guides and helpful articles that help his 40,000+ monthly readers find answers, solve problems, and meet their curious needs.

1 thought on “CS50 PSet 1: Hello Solution”

Leave a Comment

Slide to prove you're not a bot/spammer *