Questions
-
Rewrite the following
if
statements into a singleif ... then ... else
statement: -
What is the output from the following code?
-
Write an appropriate selection statement/construct to do the following:
- If an integer variable
currentNum
is odd, change its value so it is now \(3\) times thecurrentNum
+ \(1\), otherwise change its value so that it is now half the value ofcurrentNum
(rounded down whencurrentNum
is odd) - If an integer variable
n
has the value \(1\), read indouble
values forX
andY
, calculate and print their sum - Assign a value to a
double
variablecost
depending on the integer variabledistance
as in the following table:
- If an integer variable
Distance | Cost |
---|---|
0 through 100 | 5.00 |
More than 100 but not more than 500 | 8.00 |
More than 500 but less then 1,000 | 10.00 |
1,000 or more | 12.00 |
Additional Exercises
- Write an if-statement that takes two integer variables and exchanges their values if the first one is greater than the second one.
- Write a program that asks for a digit (0-9), and depending on the input, shows the digit as a word (in English) using a switch statement.
- A year is a leap year if it is divisible by 4, unless it is a century year, in which case it is only a leap year if it is divisible by 400 (e.g. 2000 is a leap year, but 1900 was not). Design and write a program to accept a year and output a message indicating if it is a leap year.
-
(something to stretch the Maths muscles ...) Write a program that gets the coefficients \(a\), \(b\) and \(c\) of a quadratic equation: \(ax^{2} + bx + c\), calculates and prints its real roots (if they exist).
Tip
A quadratic equation may have one or two real roots or no real roots at all. In order to calculate the real roots of a given quadratic equation, we first calculate the discriminant (\(D\)) by the formula: \(D = b^{2} - 4ac\). If the discriminant (\(D\)) is zero, then the quadratic equation has one double real root and it is calculated by the formula: \(x_{1,2} = \frac{-b}{2a}\). If the value of the discriminant is positive, then the equation has two distinct real roots, which are calculated by the formula: \(x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2a}\). If the discriminant is negative, the quadratic equation has no real roots). Quadratic equations may have 0, 1 or 2 real roots. The
Math
namespace has a square root method:Math.Sqrt()
. -
READ and DO the tutorial on iteration