✍️ Practice (Easy)
1. Temperature
Complete the following sample of code given the statement below and print a message when the condition is met:
If the temperature is below zero, display it's freezing.
double temperature = 3.0;
// Your answer goes here
👀Hint
What comparison operator is needed to express "less than"?
💡Solution
double temperature = 3.0;
if (temperature < 0)
{
Console.WriteLine("It's freezing!");
}
2. Passing Grade
Complete the following sample of code given the statement below and print a message when the condition is met:
If a student's grade is 60 or above, display that they passed; otherwise, display that they failed.
int grade = 55;
// Your answer goes here
👀Hint
This needs two outcomes, one for each possibility. Which statement lets you handle both?
💡Solution
int grade = 55;
if (grade >= 60)
{
Console.WriteLine("You passed!");
}
else
{
Console.WriteLine("You failed.");
}
3. Movie Rating
Complete the following sample of code given the statement below and print a message when the condition is met:
If a person's age is under 13, display that the movie is not recommended; if their age is 13 to 17, display that parental guidance is suggested; otherwise, display that the movie is fine to watch.
int age = 15;
// Your answer goes here
👀Hint
There are three possible outcomes here. Which two keywords let you chain more than one condition together?
💡Solution
int age = 15;
if (age < 13)
{
Console.WriteLine("Not recommended.");
}
else if (age < 18)
{
Console.WriteLine("Parental guidance suggested.");
}
else
{
Console.WriteLine("Fine to watch.");
}
4. Scholarship
Complete the following sample of code given the statement below and print a message when the condition is met:
Display "you are eligible for a scholarship" if the age of a person is between 18 and 25 (inclusive).
int age = 19;
// Your answer goes here
👀Hint
Both a lower and an upper bound must be true at the same time. Which logical operator combines two conditions? Remember that "inclusive" means 18 and 25 themselves qualify.
💡Solution
int age = 19;
if (age >= 18 && age <= 25)
{
Console.WriteLine("You are eligible for a scholarship.");
}
5. Percentage of Increase
Complete the following sample of code given the statement below and print a message when the condition is met:
Ensure that the percentage of increase is between -100.0 and +100.0. If it's the case, calculate the increase amount.
int stockValue = 100000;
double increasePercent = 150.0;
// Your answer goes here
👀Hint
Check both bounds with a logical operator before calculating. The increase amount is the stock value multiplied by the percentage divided by 100.
💡Solution
int stockValue = 100000;
double increasePercent = 150.0;
if (increasePercent >= -100.0 && increasePercent <= 100.0)
{
double increaseAmount = stockValue * increasePercent / 100;
Console.WriteLine($"The increase amount is {increaseAmount}.");
}