You’ve learned how to generate random numbers and control your program’s flow with if, else if, and else. In this exercise, you put it all together by writing a program that displays a subscription renewal message based on a randomly generated number of days until expiration.
This exercise takes approximately 20 minutes.
You’ll write and run your code using an online C# editor — no installation required.
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:
// Generate a random number of days until expiration
// Display an expiration message based on the days remaining
// Display a discount message if a discount applies
Remember, comments are ignored by C# when the program runs. They’re just there to help you organize your code.
Beneath the // Generate a random number of days until expiration comment, add the following lines:
Random random = new Random();
int daysUntilExpiration = random.Next(12);
int discountPercentage = 0;
random.Next(12) returns a random whole number from 0 up to, but not including, 12 — so daysUntilExpiration will be a number from 0 to 11. discountPercentage starts at 0 and you’ll update it later, depending on how urgent the renewal is.
Now you’ll add an if, else if, else chain that checks daysUntilExpiration and displays a message. C# checks each condition from top to bottom and only runs the first one that matches, so you need to order your conditions from the most urgent to the least urgent.
Beneath the // Display an expiration message based on the days remaining comment, add code that checks the following rules, in this order:
daysUntilExpiration is 0, display: Your subscription has expired.daysUntilExpiration is 1, display: Your subscription expires within a day! and set discountPercentage to 20daysUntilExpiration is 5 or less, display: Your subscription expires in {daysUntilExpiration} days. and set discountPercentage to 10daysUntilExpiration is 10 or less, display: Your subscription will expire soon. Renew now!Tip: Use one
iffor the first rule,else iffor the next three rules, and a finalelsefor the last rule. SincedaysUntilExpirationis random, a value like3should match rule 3 (5or less) — but only because rules 1 and 2 didn’t match first.
daysUntilExpiration is random, you’ll need multiple runs to see each message. Confirm that:
0 shows the expired message1 shows the “within a day” message2 to 5 show the “expires in X days” message6 to 10 show the “will expire soon” message11 shows nothingFinally, you’ll display a discount message — but only when a discount was actually set in the previous section.
Beneath the // Display a discount message if a discount applies comment, add a separate if statement (not else if) that checks whether discountPercentage is greater than 0, and if so, displays: Renew now and save {discountPercentage}%.
Tip: This needs to be its own
ifstatement, outside the chain you wrote above. That’s because the discount message depends on the value ofdiscountPercentage, not directly ondaysUntilExpiration.
Select ▶️ Run several times until you see a run where daysUntilExpiration is 1 or between 2 and 5, and confirm the discount message appears beneath the expiration message.
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.
// Generate a random number of days until expiration
Random random = new Random();
int daysUntilExpiration = random.Next(12);
int discountPercentage = 0;
// Display an expiration message based on the days remaining
if (daysUntilExpiration == 0)
{
Console.WriteLine("Your subscription has expired.");
}
else if (daysUntilExpiration == 1)
{
Console.WriteLine("Your subscription expires within a day!");
discountPercentage = 20;
}
else if (daysUntilExpiration <= 5)
{
Console.WriteLine($"Your subscription expires in {daysUntilExpiration} days.");
discountPercentage = 10;
}
else if (daysUntilExpiration <= 10)
{
Console.WriteLine("Your subscription will expire soon. Renew now!");
}
// Display a discount message if a discount applies
if (discountPercentage > 0)
{
Console.WriteLine($"Renew now and save {discountPercentage}%.");
}
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#, is there another way to write a long chain of if/else if statements? Can you show me a beginner friendly example using a switch statement?”
After the AI responds: Take a look at the examples the AI gives you. Do you see how a switch statement might replace part of your if/else if chain? You don’t need to change your program — just compare the two approaches.
AI Prompt:
“In C#, how can I combine multiple comparisons into a single if condition using logical operators? Can you show me beginner friendly examples?”
After the AI responds: Take a look at the examples the AI gives you. Can you rewrite the “5 days or less” rule so it explicitly checks both the lower and upper bounds (for example, more than 1 day, but 5 or fewer)? Try it out in your program and see if it still behaves the same way.
Once your basic version works, try adding a new rule that offers an extra discount (like 30%) when a subscription has already expired, to encourage the user to renew anyway.