C# Variables Challenge Solution: Console Output
Here’s a review of a working solution for the Module 2 challenge, which shows how to combine literal strings with variables in C#:
string name = "Bob"; // Store the user's name
int messages = 3; // Number of messages
decimal temperature = 34.4m; // Temperature as decimal
Console.Write("Hello, ");
Console.Write(name);
Console.Write("! You have ");
Console.Write(messages);
Console.Write(" messages in your inbox. The temperature is ");
Console.Write(temperature);
Console.Write(" celsius.");
✅ Output:
Hello, Bob! You have 3 messages in your inbox. The temperature is 34.4 celsius.
Key Points from This Solution
-
Variable Initialization
-
string name→ stores"Bob" -
int messages→ stores the number3 -
decimal temperature→ stores34.4m
-
-
Using Console.Write()
-
Multiple
Console.Write()statements were used to combine text with variables. -
You could also use
Console.WriteLine()string interpolation for a cleaner solution:
-
Console.WriteLine($"Hello, {name}! You have {messages} messages in your inbox. The temperature is {temperature} celsius.");
-
Flexibility
-
There’s more than one way to solve this challenge.
-
The main requirement is to store values in the correct variable types and display them correctly.
-
Tips for Module 2 Success
-
Use descriptive variable names for readability.
-
Choose the proper data type:
string,int,decimal,float, ordouble. -
Explore string interpolation for modern, concise C# coding.
This review prepares you for the next unit knowledge check. If you had trouble completing this challenge, it’s recommended to revisit previous units before moving forward.
Continue learning
Further reading: Microsoft documentation.
Review the variables challenge solution
Must my code use exactly the same variable names as the example?
Follow any explicit naming requirements in the challenge. Otherwise, descriptive names and correct types matter more than matching a sample's spelling. Different implementations can produce the same output, but hard-coding the final sentence does not demonstrate variable use.
Practice and check
Change the name, message count and temperature separately. Each change should affect only the corresponding part of the displayed message. This catches solutions that accidentally print a literal instead of reading the variable.
Comments
Post a Comment