In this exercise, you write a C# program that calculates a tip and total for a restaurant bill. You practice storing values in variables, performing arithmetic operations, and converting user input from text to numbers so you can use it in calculations.
This exercise takes approximately 20 minutes.
Open a browser and navigate to the C# editor at https://microsoftlearning.github.io/c-sharp-minor.
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.
Copy the following comments and paste them into the editor pane:
// Collect the bill amount and tip percentage
// Display the entered values
// Calculate the tip and total
// Display the results
Remember, comments are ignored by C# when the program runs. They’re just there to help you organize your code.
Leave the code as-is for now. In the steps that follow, you’ll add code beneath each comment.
You’ll start by storing the bill amount and tip percentage as hardcoded variables.
Beneath the // Collect the bill amount and tip percentage comment, add the following lines:
decimal billAmount = 45.50m;
decimal tipPercentage = 18.0m;
Each variable is a decimal — a data type suited for precise values like money. The m after each number is a suffix that tells C# to treat the literal as a decimal instead of a double.
Beneath the // Display the entered values comment, add the following lines:
Console.WriteLine("Bill details:");
Console.WriteLine($" Bill amount: ${billAmount}");
Console.WriteLine($" Tip percentage: {tipPercentage}%");
Select the ▶️ Run button to run the program. You should see:
Bill details:
Bill amount: $45.50
Tip percentage: 18.0%
Select the Clear console button before you continue.
Now you’ll use arithmetic to calculate the tip amount and the total bill, using the formula:
\[tip = billAmount \times \frac{tipPercentage}{100}\]Beneath the // Calculate the tip and total comment, add the following lines of code:
decimal tipAmount = billAmount * (tipPercentage / 100);
decimal totalAmount = billAmount + tipAmount;
Beneath the // Display the results comment, add the following lines of code:
Console.WriteLine($"\nTip amount: ${tipAmount}");
Console.WriteLine($"Total amount: ${totalAmount}");
Note:
\ninside an interpolated string adds a blank line before the text, which helps separate sections of output.
Select ▶️ Run. Your complete output should now look like this:
Bill details:
Bill amount: $45.50
Tip percentage: 18.0%
Tip amount: $8.190
Total amount: $53.690
Select Clear console before you continue.
Hardcoded values are useful for testing, but a real program should accept input from the user. Console.ReadLine() always returns a string, so you need to convert it to a decimal before you can use it in calculations.
Replace the two hardcoded variables beneath the // Collect the bill amount and tip percentage comment with the following code:
Console.WriteLine("Enter your bill details:\n");
Console.Write("Bill amount: ");
decimal billAmount = Convert.ToDecimal(Console.ReadLine());
Console.Write("Tip percentage: ");
decimal tipPercentage = Convert.ToDecimal(Console.ReadLine());
Wrapping Console.ReadLine() inside Convert.ToDecimal() converts the text the user types into a decimal number in a single step.
Keep the rest of your code exactly as it is. Your complete program should now look like this:
// Collect the bill amount and tip percentage
Console.WriteLine("Enter your bill details:\n");
Console.Write("Bill amount: ");
decimal billAmount = Convert.ToDecimal(Console.ReadLine());
Console.Write("Tip percentage: ");
decimal tipPercentage = Convert.ToDecimal(Console.ReadLine());
// Display the entered values
Console.WriteLine("\nBill details:");
Console.WriteLine($" Bill amount: ${billAmount}");
Console.WriteLine($" Tip percentage: {tipPercentage}%");
// Calculate the tip and total
decimal tipAmount = billAmount * (tipPercentage / 100);
decimal totalAmount = billAmount + tipAmount;
// Display the results
Console.WriteLine($"\nTip amount: ${tipAmount}");
Console.WriteLine($"Total amount: ${totalAmount}");
Select ▶️ Run. When each prompt appears in the output console, click on it, type a value, and press Enter.
Try entering the same values as before to verify your output matches the following:
Enter your bill details:
Bill amount: 45.50
Tip percentage: 18
Bill details:
Bill amount: $45.50
Tip percentage: 18%
Tip amount: $8.190
Total amount: $53.690
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.
AI Prompt:
“In C#, how can I display a decimal value as currency, with a dollar sign and exactly two decimal places? Can you show me beginner friendly examples?”
After the AI responds: Take a look at the examples the AI gives you. Do you see a way to clean up the $53.690 in your output so it displays as $53.69? Try it out in your program and see if it works as expected.
AI Prompt:
“In C#, how can I round a decimal value to two decimal places without using currency formatting? Can you show me beginner friendly examples?”
After the AI responds: Take a look at the examples the AI gives you. Can you find a way to round your tip amount before displaying it? Try it out in your program and see if it works as expected.
AI Prompt:
“In C#, what are some common methods I can use to compare numbers and find the larger or smaller of two values? Can you show me beginner friendly examples?”
After the AI responds: Take a look at the examples the AI gives you. Can you find a way to compare this bill’s total to a previous total and display the larger one? Try it out in your program and see if it works as expected.