0% found this document useful (0 votes)
7 views

Tutorial 04

Uploaded by

Ranidu Ravishka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Tutorial 04

Uploaded by

Ranidu Ravishka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming with C Language

Tutorial 04 – Writing if condition

1) What is wrong with the following if statement (there are at least 3 errors). The
Indentation indicates the desired behavior.
if numNeighbors >= 3 || numNeighbors = 4
++numNeighbors;
printf("You are dead! \n " );
else
--numNeighbors;

I. The condition `numNeighbors = 4` should be `numNeighbors == 4` to check for


equality.
II. The increment operator `++numNeighbors` and the decrement operator `--
numNeighbors` should be enclosed in curly braces `{}` to define the scope of the if
and else blocks.
III. The `printf` statement should also be enclosed in curly braces `{}` to be part of the if
block.
Here's the corrected version of the if statement:

if (numNeighbors >= 3 || numNeighbors == 4) {


++numNeighbors;
printf("You are dead! \n");
} else {
--numNeighbors;
}

2) Describe the output produced by this poorly indented program segment:


int number = 4;
double alpha = -1.0;
if (number > 0)
if (alpha > 0)
printf("Here I am! \n" );
else
printf("No, I’m here! \n");
printf(“No, actually, I’m here! \n");

“No, actually, I’m here!”

3) Consider the following if statement, where doesSignificantWork,


makesBreakthrough, and nobelPrizeCandidate are all boolean variables:

if (doesSignificantWork) {
if (makesBreakthrough)
nobelPrizeCandidate = true;
else
nobelPrizeCandidate = false;
}
else if (!doesSignificantWork)
nobelPrizeCandidate = false;

In the given if statement, the value of the boolean variable `nobelPrizeCandidate` is


determined based on the values of `doesSignificantWork` and `makesBreakthrough`. Here's
how it works:
- If `doesSignificantWork` is true:
- If `makesBreakthrough` is true, then `nobelPrizeCandidate` is set to true.
- If `makesBreakthrough` is false, then `nobelPrizeCandidate` is set to false.
- If `doesSignificantWork` is false, then `nobelPrizeCandidate` is always set to false.
So, the value of `nobelPrizeCandidate` depends on whether significant work is done and
whether a breakthrough is made. If significant work is done and a breakthrough is made, the
variable will be true. Otherwise, it will be false.
4) Write if statements to do the following:

1. If character variable taxCode is ’T’, increase price by adding the taxRate percentage
of price to it.
if (taxCode == 'T') { price += (taxRate / 100) * price;
}

2. If integer variable opCode has the value 1, read in double values for X and Y and
calculate and print their sum.
if (opCode == 1) {
double X, Y;
printf("Enter value for X: ");
scanf("%lf", &X);
printf("Enter value for Y: ");
scanf("%lf", &Y);
double sum = X + Y;
printf("The sum of X and Y is: %.2lf\n", sum);
}

3. If integer variable currentNumber is odd, change its value so that it is now 3 times
currentNumber plus 1, otherwise change its value so that it is now half of
currentNumber (rounded down when currentNumber is odd).
if (currentNumber % 2 != 0) {
currentNumber = (3 * currentNumber) + 1;
} else {
currentNumber = currentNumber / 2;
}
4. Assign true to the boolean variable leapYear if the integer variable year is a leap
year. (A leap year is a multiple of 4, and if it is a multiple of 100, it must also be a
multiple of 400.)
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
leapYear = 1;
} else {
leapYear = 0;
}

5. Assign a value to double variable cost depending on the value of integer variable
distance as follows:
if (distance <= 100) {
cost = 5.00;
} else if (distance <= 500) {
cost = 8.00;
} else if (distance < 1000) {
cost = 10.00;
} else {
cost = 12.00;
}

Distance Cost
----------------------------------- ----------
0 through 100 5.00
More than 100 but not more than 500 8.00
More than 500 but less than 1,000 10.00
1,000 or more 12.00

You might also like