Skip to content

Commit 7521b8a

Browse files
committed
Added exercises for LP1
1 parent 79d861f commit 7521b8a

7 files changed

Lines changed: 1248 additions & 0 deletions
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
---
3+
lab:
4+
title: 'Create a personalized greeting'
5+
description: 'Write a C# program that uses Console.WriteLine(), Console.ReadLine(), and string interpolation to display a personalized greeting message.'
6+
level: 100
7+
duration: 15
8+
islab: true
9+
status: 'released'
10+
---
11+
12+
# Create a personalized greeting
13+
14+
In this exercise, you write a C# program that asks for the user's name and displays a personalized greeting. You practice using the `Console.WriteLine()` method to display output, the `Console.ReadLine()` method to collect user input, and string interpolation to format a custom message.
15+
16+
This exercise takes approximately **15** minutes.
17+
18+
## Open the online C# IDE
19+
20+
You'll write and run your code using an online C# editor — no installation required.
21+
22+
1. Open a browser and navigate to the C# editor at [https://microsoftlearning.github.io/c-sharp-minor](https://microsoftlearning.github.io/c-sharp-minor).
23+
24+
2. You'll see two panels:
25+
- **Code Editor pane** (top): where you write your C# code.
26+
- **Output Console** (bottom): where output is displayed and where you can type input when the program asks for it.
27+
28+
3. The editor may contain some default code. Select all of it and delete it so you're starting with a clean, empty file.
29+
30+
## Set up your program
31+
32+
Before writing any logic, you'll paste a set of guiding comments into the editor. These comments act as an outline for your program — each one marks where a specific piece of code belongs.
33+
34+
1. Copy the following comments and paste them into the editor pane:
35+
36+
```csharp
37+
// Display a welcome message
38+
39+
// Ask for the user's name
40+
41+
// Display a personalized greeting
42+
```
43+
44+
Remember, comments are ignored by C# when the program runs. They're just there to help you organize your code.
45+
46+
## Display a welcome message
47+
48+
The first thing your program should do is greet the user when it starts. You'll use the `Console.WriteLine()` method to display a message on the screen.
49+
50+
1. In the editor pane, add the following line beneath the `// Display a welcome message` comment:
51+
52+
```csharp
53+
Console.WriteLine("Welcome to the greeting program!");
54+
```
55+
56+
1. Select the ▶️ **Run** button to run the code.
57+
58+
1. Check the output console. You should see:
59+
60+
```output
61+
Welcome to the greeting program!
62+
```
63+
64+
> **Note**: If nothing appears, make sure the code is typed exactly as shown, including the quotes, parentheses, and semicolon.
65+
66+
1. Select the **Clear console** button to clear the output before you continue.
67+
68+
## Ask for the user's name
69+
70+
Now you'll add code that pauses the program and asks for the user's name. You'll use `Console.Write()` to display a prompt without moving to a new line, and `Console.ReadLine()` to wait for the user to type a response.
71+
72+
1. Beneath the `// Ask for the user's name` comment, add the following lines:
73+
74+
```csharp
75+
Console.Write("What is your name? ");
76+
string name = Console.ReadLine();
77+
```
78+
79+
The first line displays the prompt `What is your name? ` in the console. The second line waits for the user to type a response and stores whatever they type into a variable called `name`.
80+
81+
1. Run the program again by selecting ▶️ **Run**.
82+
83+
1. When `What is your name? ` appears in the output console, click on the console and type your name, then press **Enter**.
84+
85+
1. The program finishes after you enter your name — but it doesn't say anything yet. You'll fix that in the next step.
86+
87+
1. Select **Clear console** before you continue.
88+
89+
## Display a personalized greeting
90+
91+
Now you'll use the `name` variable to build a personalized message. You'll do this with **string interpolation** — a technique that lets you embed variable values directly inside a string by prefixing the string with `$` and wrapping variable names in curly braces `{}`.
92+
93+
1. Beneath the `// Display a personalized greeting` comment, add the following line:
94+
95+
```csharp
96+
Console.WriteLine($"Hello, {name}! It's great to meet you.");
97+
```
98+
99+
1. Run the program, enter your name when prompted, and press **Enter**.
100+
101+
1. You should see output similar to:
102+
103+
```output
104+
Welcome to the greeting program!
105+
What is your name? Alex
106+
Hello, Alex! It's great to meet you.
107+
```
108+
109+
1. Your complete program should now look like this:
110+
111+
```csharp
112+
// Display a welcome message
113+
Console.WriteLine("Welcome to the greeting program!");
114+
115+
// Ask for the user's name
116+
Console.Write("What is your name? ");
117+
string name = Console.ReadLine();
118+
119+
// Display a personalized greeting
120+
Console.WriteLine($"Hello, {name}! It's great to meet you.");
121+
```
122+
123+
1. Run the program one more time and confirm the output matches what you entered. For example:
124+
125+
```output
126+
Welcome to the greeting program!
127+
What is your name? Alex
128+
Hello, Alex! It's great to meet you.
129+
```
130+
131+
## Code with AI
132+
133+
Using an AI assistant (like Copilot) is a great way to explore a programming language at your own pace. Try the prompt below, read the response carefully, and test the code examples it gives you in the online editor.
134+
135+
### Discovery: Transforming text
136+
137+
**AI Prompt:**
138+
```In C#, how can I change a string input to all uppercase or all lowercase? Can you show me examples?```
139+
140+
**After the AI responds:** Look closely at the code examples it provides. Try modifying your greeting program to display the user's name in uppercase or lowercase.
141+
142+
<details>
143+
<summary>Show answer</summary>
144+
You can use the following string methods to transform text:
145+
146+
```csharp
147+
string name = Console.ReadLine();
148+
149+
// Converts to all uppercase
150+
string uppercaseName = name.ToUpper();
151+
152+
// Converts to all lowercase
153+
string lowercaseName = name.ToLower();
154+
```
155+
</details>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
lab:
3+
title: 'Challenge: Interact with the user'
4+
description: 'Write a C# program that combines Console.Write(), Console.ReadLine(), and string interpolation to collect multiple pieces of information from the user and display a personalized message.'
5+
level: 100
6+
duration: 15
7+
islab: true
8+
status: 'released'
9+
---
10+
11+
# Challenge: Interact with the user
12+
13+
You've learned the basics of C# console output, user input, and string manipulation. In this exercise, you put it all together by writing a program that asks the user two questions and combines their answers into a single personalized message.
14+
15+
This exercise takes approximately **15** minutes.
16+
17+
## Open the online C# IDE
18+
19+
You'll write and run your code using an online C# editor — no installation required.
20+
21+
1. Open a browser and navigate to the C# editor at [https://microsoftlearning.github.io/c-sharp-minor](https://microsoftlearning.github.io/c-sharp-minor).
22+
23+
2. You'll see two panels:
24+
- **Code Editor pane** (top): where you write your C# code.
25+
- **Output Console** (bottom): where output is displayed and where you can type input when the program asks for it.
26+
27+
3. The editor may contain some default code. Select all of it and delete it so you're starting with a clean, empty file.
28+
29+
## Set up your program
30+
31+
Before writing any logic, you'll paste a set of guiding comments into the editor. These comments act as an outline for your program — each one marks where a specific piece of code belongs.
32+
33+
1. Copy the following comments and paste them into the editor pane:
34+
35+
```csharp
36+
// Ask for the user's name
37+
38+
// Ask for the user's age
39+
40+
// Display a personalized message that includes both pieces of information
41+
```
42+
43+
Remember, comments are ignored by C# when the program runs. They're just there to help you organize your code.
44+
45+
## Ask for the user's name
46+
47+
1. Beneath the `// Ask for the user's name` comment, add code that:
48+
- Uses `Console.Write()` to display the prompt `What is your name? ` (remember the space before the closing quote, so the user's answer doesn't run into the prompt)
49+
- Uses `Console.ReadLine()` to read the user's response into a variable named `name`
50+
51+
> **Tip**: If you get stuck, look back at how you asked for the user's name in the [Create a personalized greeting](01-exercise-greeting.md) exercise.
52+
53+
1. Select the ▶️ **Run** button to run the code so far, and type your name when prompted.
54+
55+
1. Select the **Clear console** button to clear the output before you continue.
56+
57+
## Ask for the user's age
58+
59+
1. Beneath the `// Ask for the user's age` comment, add code that:
60+
- Uses `Console.Write()` to display the prompt `How old are you? `
61+
- Uses `Console.ReadLine()` to read the user's response into a variable named `age`
62+
63+
> **Note**: `Console.ReadLine()` always returns a `string`, even if the user types a number. That's fine for this exercise — you can use the `age` variable directly inside a string interpolation without converting it to a number first.
64+
65+
1. Run the program again, and answer both prompts when asked.
66+
67+
1. Select **Clear console** before you continue.
68+
69+
## Display a personalized message
70+
71+
1. Beneath the `// Display a personalized message that includes both pieces of information` comment, add a `Console.WriteLine()` statement that uses string interpolation (`$"..."`) to combine the `name` and `age` variables into a single message.
72+
73+
1. Run your program and check that the output looks similar to this, using the name and age you entered:
74+
75+
```output
76+
What is your name? Sam
77+
How old are you? 25
78+
Hello, Sam! You are 25 years old.
79+
```
80+
81+
> **Note**: If you get an error, check that you have double quotes around each piece of text, a semicolon at the end of each statement, and that `Console`, `Write`, `WriteLine`, and `ReadLine` are capitalized correctly.
82+
83+
1. Select **Clear console** before you continue.
84+
85+
## Verify your solution
86+
87+
Compare your program to the example below. Your code doesn't need to match exactly — there are many ways to write a program that works correctly — but the behavior should be the same.
88+
89+
```csharp
90+
// Ask for the user's name
91+
Console.Write("What is your name? ");
92+
string name = Console.ReadLine();
93+
94+
// Ask for the user's age
95+
Console.Write("How old are you? ");
96+
string age = Console.ReadLine();
97+
98+
// Display a personalized message that includes both pieces of information
99+
Console.WriteLine($"Hello, {name}! You are {age} years old.");
100+
```
101+
102+
## Code with AI
103+
104+
Using an AI assistant (like Copilot) is a great way to explore a programming language at your own pace. Try each prompt below, read the response carefully, and test the code examples it gives you in the online editor.
105+
106+
### Discovery 1: Printing a decorative banner
107+
108+
**AI Prompt:**
109+
```In C#, how can I print a line of 20 dashes in a row without typing them all out? Can you show me an example?```
110+
111+
**After the AI responds:** Try using the technique it suggests to print a banner line above and below your program's final greeting.
112+
113+
<details>
114+
<summary>Show answer</summary>
115+
You can use the `new string(char, count)` constructor to repeat a character multiple times. For example, to print 20 dashes in a row:
116+
117+
```csharp
118+
Console.WriteLine(new string('-', 20));
119+
Console.WriteLine($"Hello, {name}! You are {age} years old.");
120+
Console.WriteLine(new string('-', 20));
121+
```
122+
</details>
123+
124+
### Discovery 2: Formatting a multi-line message
125+
126+
**AI Prompt:**
127+
```In C#, how can I display a message that spans multiple lines using a single Console.WriteLine() statement? Can you show me examples?```
128+
129+
**After the AI responds:** Try modifying your program's final message so the name and age each appear on their own line.
130+
131+
<details>
132+
<summary>Show answer</summary>
133+
You can use the newline escape character `\n` inside a regular string, or a verbatim/raw string that spans multiple lines. For example:
134+
135+
```csharp
136+
Console.WriteLine($"Name: {name}\nAge: {age}");
137+
```
138+
</details>

0 commit comments

Comments
 (0)