If Else Practice Statements
If Else Practice Statements
(Odd or Even) Write a program that reads an integer and determines and prints whether it’s
odd or even.
[Hint: Use the modulus operator. An even number is a multiple of two. Any multiple of two
leaves a remainder of zero when divided by 2.]
(BMI) Create a BMI calculator application that reads the user’s weight in pounds and
height in inches (or, if you prefer, the user’s weight in kilograms and height in meters),
then calculates and displays the user’s body mass index. Also, the application should
display the following information from the Department of Health and Human
Services/National Institutes of Health so the user can evaluate his/her BMI.
BMI VALUES:
Underweight: less than 18.5
Normal: between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese: 30 or greater
Question 1.
Identify whether the following lines of code contain errors or not, also state the error
your compiler generates.
1a.
static void Main(string[] args)
{
int num=5;
If (num=9)
{
Console.WriteLine(“the number is {0}”, num);
}
else
{
Console.WriteLine(“the number is {0}”, num);
}
}
1b.
static void Main(string[] args)
{
If (2)
{
Console.WriteLine(“******”);
}
else
{
Console.WriteLine(“######”);
}
}
1c.
static void Main(string[] args)
{
If (-45)
{
Console.WriteLine(“******”);
}
else
{
Console.WriteLine(“######”);
}
}
Dangling if-else problem:
State the output of the code
1.
static void Main(string[] args)
{
int x = 9, y = 11;
if (x < 10)
if (y > 10)
Console.WriteLine("*****");
else
Console.WriteLine("#####");
Console.WriteLine("$$$$$");
}
2.
static void Main(string[] args)
{
if (x < 10)
if (y > 10)
Console.WriteLine("*****");
else
Console.WriteLine("#####");
Console.WriteLine("$$$$$");
}
if (y == 8)
if (x == 5)
Console.WriteLine("@@@@@");
else
Console.WriteLine("#####");
Console.WriteLine("$$$$$");
Console.WriteLine("&&&&&");