CS50 Hello, It’s Me Solution

By Suraj Chaudhary

Are you looking for the solution of Harvard’s CS50 PSet 1? Do you want the Hello solution of CS50?

If so you’re in the right place.

In this article, I’m going to share with you exactly how to solve the PSet and also share with you the solution code.

When you start taking CS50, Hello, Mario (less or more), cash, and/or credit are some of the first questions that you will have to submit.

Here’s how to solve the hello problem in CS50.

Read the given paragraphs to totally grasp the hello solution and be able to do it on your own.

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 the hello problem of CS50? Here’s how:

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

int main(void)
{
    // Ask for the name
    string name = get_string("What's your name? ");
    
    // Print hello with the given name
    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. A

fter 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 use Windows 11 on Mac for Free
  3. How To Get A Domain Name For FREE
  4. How To Use Microsoft Office Apps For FREE Legally (Mac & PC)

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resource to living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

1 thought on “CS50 Hello, It’s Me Solution”

Leave a Comment

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