✍️ Practice (Medium)
1. Ascending
Given two numbers, print them in ascending order (smallest first).
int num1 = 8;
int num2 = 3;
// Your answer goes here
👀Hint
You should write a conditional statement that prints the two numbers in a different order, depending on which one is bigger.
💡Solution
int num1 = 8;
int num2 = 3;
if (num1 <= num2)
{
Console.WriteLine(num1 + " " + num2);
}
else
{
Console.WriteLine(num2 + " " + num1);
}
2. Temperature Ranges
Write a program which asks the user for the temperature. The program prints a different message for each of the following conditions:
- above 25: it's hot outside
- 25 or below, but above 15: it's pretty warm outside
- 15 or below, but 0 or above: it's pretty chill outside
- below 0: it's freezing outside
👀Hint
There are four possible outcomes, and only one message should ever print. Start with the highest range and work your way down. Which keywords let you chain the checks so that only the first true one runs?
💡Solution
Console.WriteLine("What's the temperature?");
double temperature = double.Parse(Console.ReadLine());
if (temperature > 25)
{
Console.WriteLine("It's TOO hot outside 🏜️");
}
else if (temperature > 15)
{
Console.WriteLine("It's pretty warm outside ☀️");
}
else if (temperature >= 0)
{
Console.WriteLine("It's pretty chilly outside 🍂");
}
else
{
Console.WriteLine("It's freezing outside 🥶");
}
3. Arcane
⚠️ For those who never watched this before, you have homework
I can binge Arcane if I answered all my emails by 5pm and the internet is working, otherwise if I still have emails left, I’ll answer those instead. If the internet’s down tho, I’ll probably need to call hydro quebec lol.
👀Hint
There are three possible outcomes, and the first one needs three things to be true at once. Which logical operator joins them?
💡Solution
double time = 15.50; // 24 hour clock lol
int numEmails = 30;
bool hasInternet = true;
if (numEmails <= 0 && time >= 17.00 && hasInternet)
{
Console.WriteLine("Everyone wants to be my enemy 🎶");
}
else if (numEmails > 0 && time >= 17.00 && hasInternet)
{
Console.WriteLine("The eternal email grind ✉️");
}
else if (time >= 17.00 && !hasInternet)
{
Console.WriteLine("Excuse me hydro quebec, i have an issue ☎️");
}
4. Oil Change
Print "Change your car oil" if a car's mileage exceeds 5000 km or it's been more than 3 months since the last oil change. Additionally, there must be at least $80.00 of savings.
double savings = 120.00;
int mileage = 5400;
int months = 2;
// Your answer goes here
👀Hint
You can chain multiple conditions in one if statement using and/or.
💡Solution
double savings = 120.00;
int mileage = 5400;
int months = 2;
if ((mileage > 5000 || months > 3) && savings >= 80.00)
{
Console.WriteLine("Change your car oil 🛢️");
}
5. Ascending (Again)
Given three numbers, print them in ascending order (smallest first). Try to do it without nesting any if statements.
int num1 = 8;
int num2 = 3;
int num3 = 5;
// Your answer goes here
👀Hint
Three numbers can be arranged in 6 different orders. Write one condition per ordering, using && to check that the numbers are in that order. Chain them with else if so only one line prints.
💡Solution
int num1 = 8;
int num2 = 3;
int num3 = 5;
if (num1 <= num2 && num2 <= num3)
{
Console.WriteLine(num1 + " " + num2 + " " + num3);
}
else if (num1 <= num3 && num3 <= num2)
{
Console.WriteLine(num1 + " " + num3 + " " + num2);
}
else if (num2 <= num1 && num1 <= num3)
{
Console.WriteLine(num2 + " " + num1 + " " + num3);
}
else if (num2 <= num3 && num3 <= num1)
{
Console.WriteLine(num2 + " " + num3 + " " + num1);
}
else if (num3 <= num1 && num1 <= num2)
{
Console.WriteLine(num3 + " " + num1 + " " + num2);
}
else
{
Console.WriteLine(num3 + " " + num2 + " " + num1);
}