More Decisions, Mastering if-else

1. Trace the flow of above example program for following user inputs

  1. score = 98.2, years = 2.3
  2. score = 88, years = 5
  3. score = 70, years = 10
  4. score = 79, years = 1.2
#include <stdio.h>

int main (int argc, char * argv[]) {
    double score = 0, years = 0;
    printf("Enter your score: ");
    scanf("%lf", &score);
    printf ("Enter years of experience: ");
    scanf("%lf", &years);
    if (score >= 80 && years >=3) {
        printf("You are eligible for scholarship\n");
    } else {
        printf("How about a student loan?\n");
    }
    return 0;
}

Ans: score = 98.2, years = 2.3

  1. After reading user input, control reaches line 09.
  2. If condition on line 09 evaluates to false (98.2 >= 80 && 2.3 >= 3) – first operand of && evaluates to true while second operand evaluates to false. TRUE && FALSE evaluates to false. Control jumps to else block, line 12.
  3. Message How about a student loan is printed to the console.
  4. Control advances to end of else block, end of program and exits.

Ans: score = 88, years = 5

  1. After reading user input, control reaches line 09.
  2. If condition on line 09 evaluates to true (88 >= 80 && 5 >= 3) – first operand of && evaluates to true and second operand also evaluates to true. TRUE && TRUE evaluates to true. Control enter if block, line 10.
  3. Message You are eligible for scholarship is printed to the console.
  4. Control advances to end of if block, skips else block, reaches end of program and exits.

Ans: score = 70, years = 10

  1. After reading user input, control reaches line 09.
  2. If condition on line 09 evaluates to false (70 >= 80 && 10 >= 3) – first operand of && evaluates to false while second operand evaluates to true. FALSE && TRUE evaluates to false. Control jumps to else block, line 12.
  3. Message How about a student loan is printed to the console.
  4. Control advances to end of else block, end of program and exits.

Ans: score = 79, years = 1.2

  1. After reading user input, control reaches line 09.
  2. If condition on line 09 evaluates to false (79 >= 80 && 1.2 >= 3) – first operand of && evaluates to false and second operand also evaluates to false. FALSE && FALSE evaluates to false. Control jumps to else block, line 12.
  3. Message How about a student loan is printed to the console.
  4. Control advances to end of else block, end of program and exits.

 

2. What would be the output of following program.

#include <stdio.h>

int main(int argc, char * argv[]) {
    int copies_sold = 500000;
    double weeks_since_launch = 3.3;
    if (copies_sold > 5000 && weeks_since_launch < 1) {
        printf ("This is a bestseller\n");
    } else {
        printf("Not sure if this is a bestseller\n");
    }
}

if statement on line 06 evaluates to false (500000 > 5000 && 3.3 < 1) – TRUE && FALSE evaluates to false. Hence if block is skipped and else block is executed. Output printed on console is

Not sure if this is a bestseller

 

3. Trace the control flow of following program for following user inputs

200

99

0

01: #include <stdio.h>

02: int main (int argc, char * argv[]) {
03:     int runs = 0;
04:     printf("Enter runs scored by the batsman: ");
05:     scanf("%d", &runs);

06:     if (runs >= 200) {
07:         printf("You scored a double century\n");
08:     }
09:     if (runs >= 100 && runs < 200) {
10:         printf("You scored a century\n");
11:     }
12:     if (runs >= 50 && runs < 100) {
13:         printf("You scored a half century\n");
14:     }
15:     if (runs == 0) {
16:         printf("You got a duck\n");
17:     }

18:     printf("Good bye!\n");
19:     return 0;
20: }

Ans: 200

  1. After user input, control reaches if statement on line 06. Control condition evaluates to true (200 >= 200). Control enters if block, line 07.
  2. Message You score a double century is printed to the console. Control advances to end of if block.
  3. Line 09. Control condition evaluates to false (200 >= 100 && 200 < 200) – TRUE && FALSE evaluates to false. Control skips if block and reaches line 12.
  4. Control condition evaluates to false (200 >= 50 && 200 < 100) – TRUE && FALSE evaluates to false. Control skips if block and reaches line 15.
  5. Control condition evaluates to false (200 == 0). Control skips if block and reaches line 18.
  6. Message Good bye! is printed to the console. Control advances to end of program and exits.

Ans: 99

  1. After user input, control reaches if statement on line 06. Control condition evaluates to false (90 >= 200). Control skips if block and jumps to line 09.
  2. Control condition evaluates to false (99 >= 100 && 99 < 200) – FALSE && TRUE evaluates to false. Control skips if block and reaches line 12.
  3. Control condition evaluates to true (99 >= 50 && 99 < 100) – TRUE && TRUE evaluates to true. Control enters if block and reaches line 13.
  4. Message You scored a half century is printed to the console. Control reaches end of if block and reaches line 15.
  5. Control condition evaluates to false (99 == 0). Control skips if block and reaches line 18.
  6. Message Good bye! is printed to the console. Control advances to end of program and exits.

Ans: 0

  1. After user input, control reaches if statement on line 06. Control condition evaluates to false (0 >= 200). Control skips if block and jumps to line 09.
  2. Control condition evaluates to false (0 >= 100 && 0 < 200) – FALSE && TRUE evaluates to false. Control skips if block and reaches line 12.
  3. Control condition evaluates to false (0 >= 50 && 0 < 100) – FALSE && TRUE evaluates to false. Control skips if block and reaches line 15.
  4. Control condition evaluates to true (0 == 0). Control enters if block and reaches line 16.
  5. Message You got a duck is printed to the console. Control reaches end of if block and advances to line 18.
  6. Message Good bye! is printed to the console. Control advances to end of program and exits.

 

4. What will be the output of following program

#include <stdio.h>

int main (int argc, char * argv[]) {
    int a = 1, b = 2, c = 3, z = 0;
    if ( (a > (b - c)) && ((c - a) > (b - z)) ) {
        a = b - c;
        z = z + b;
    }
    if ((a <= z) && (b !=0)) {
        c = c * c;
    } else {
        z = a + b + c;
        b = z * 3;
    }
    printf("a=%d, b=%d, c=%d, z=%d", a,b,c,z);
    return 0;
}

Substituting variable values in if statement on line 05 we get

if ( (1 > (2 – 3)) && ((3 – 1) > (2 – 0)) )

which is effectively, if ( (1 > -1) && (2 > 2) )

TRUE && FALSE evaluates to false. if block is skipped. Control reaches line 09.

Substituting variable values in if statement on line 09 we get

if ( (1<=0) && (2!=0))

FALSE && TRUE evaluates to false. if block is skipped and control enters else block, line 12.

z = 1 + 2 + 3, i.e. z now has value 6 and a, b, c have 1, 2, and 3, respectively.

line 13, b = z * 3, i.e. b = 6 * 3. Now a, b, c, z have values 1, 18, 3, 6 respectively.

Hence, when control reaches printf statement on line 15, output printed is

a=1, b=18, c=3, z=6

 

5. Write a program that reads a number and prints whether the number has one digit, two digits, three digits, or more than three digits.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num = 0;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    if(num > 999) {
        printf("Number has more than 3 digits");
    }
    if(num >= 100 && num <= 999 ) {
        printf("Number has 3 digits");
    }
    if(num >= 10 && num <= 99 ) {
        printf("Number has 2 digits");
    }
    if(num >= 0 && num <= 9 ) {
        printf("Number has 2 digits");
    }

    return 0;
}

6. Assume int a = 5, b = 7, c = 11, d = 13. What is the result of following expressions.

a || b

Ans: We have learned that all non-zero values are true. Hence (5 || 7) is equivalent to (true || true) which we know evaluates to true.

(a && b) || (0 && d)

Ans: Substituting values we get, (5 && 7) || (0 && 13).

= (true && true) || (false && true)

= true || false

= true

(1 || 0) && (b > c)

Ans: Substituting values we get, (1 || 0) && (7 > 11)

= (true || false) && false

= true && false

= false

(d+b) > (a+c)

Ans: Substituting values we get, (13 + 7) > (5 + 11)

= 20 > 16

which is true.

((d+b) > (a+c)) && (0 || d<a)

Ans: Substituting values we get, ((13 + 7) > (5 + 11)) && ( 0 || 13<5)

= (20 > 16) && ( 0 || false)

= true && (false || false)

= true && false

= false

 

7. Trace the control flow of the following program for user input values mentioned below

a = 5, b = -9, c = 2, d = 10, z = 0

a = 0, b = 7, c = 1, d = 3, z = -1

a = 0, b = 0, c = 0, d = 0, z = 0

a = 2, b = 2, c = 2, d = 2, z = 2

#include <stdio.h>

int main (int argc, char * argv[]) {
    int a=0, b=0, c=0, d=0, z=0;
    printf("a = ");
    scanf("%d", &a);
    printf("b = ");
    scanf("%d", &b);
    printf("c = ");
    scanf("%d", &c);
    printf("d = ");
    scanf("%d", &d);
    printf("z = ");
    scanf("%d", &z);

    if ( (a<b) || ( ((c*d)<(b*b)) && ((a+b)<(c+d)) )){ 
        c = c + b; 
        z = b * b; 
        a = a + z; 
        b = z - b; 
    } 
    if ( (a>b) || ((z+b)>(z-d)) ) {
        b = b + 20;
        c = z - b;
    }
    printf ("a=%d, b=%d, c=%d, d=%d, z=%d\n", a,b,c,d,z);
    return 0;
}

 

8. Write a program that reads year from the user and then prints whether the input year is a leap year or not. Note that years divisible by 100 are not leap years unless they are divisible by 400. E.g. 1900 was not a leap year but 2000 was.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int year = 0;

    printf("Enter the year: ");
    scanf("%d", &year);

    if(year%4 == 0 && (year%100 != 0 || year%400 == 0)) {
        printf("%d is a leap year\n", year);
    } else {
        printf("%d is not a leap year\n", year);
    }

    return 0;
}

9. Write a program that reads in current time: hours (0-23) and minutes (0-59) separately from the user. Validate both inputs in a single control expression to make sure that the inputs are correct.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int hour = 0, mins = 0;
    printf("Enter hour: ");
    scanf("%d", &hour);
    printf("Enter minutes: ");
    scanf("%d", &mins);

    if(hour >= 0 && hour <=23 && mins >= 0 && mins <=59) {
        printf("Good input\n");
    } else {
        printf("Invalid input\n");
    }

return 0;
}

10. What will be the output of following program

#include <stdio.h>

int main (int argc, char * argv[]) {
    if (!0) {
        printf("NOT 0 is true\n");
    }
    if (!1) {
        printf("NOT 1 is true\n");
    }
    if (!-1) {
        printf("NOT -1 is true\n");
    }
    return 0;
}

We have learned that all non-zero values are true and zero is false.

Control expression on line 04 is NOT FALSE which is true.

Control expression on line 06 is NOT TRUE which is false.

Control expression on line 08 is NOT TRUE which is false. Hence output will be

NOT 0 is true

 

11. Trace the control flow of below program for following user inputs.

5

12

7

23

15

01: #include <stdio.h>

02: int main (int argc, char * argv[]) {
03:     int hour = 0;
04:     printf("Enter current hour of day (00-23): ");
05:     scanf("%d",&hour);

06:     if ((hour > 5) && (hour < 12)){
07:         printf("Good morning!\n");
08:     } else if ((hour >= 12) && (hour <= 15)){
09:         printf("Good afternoon!\n");
10:     } else if ((hour > 15) && (hour <= 21)){
11:         printf("Good evening!\n");
12:     } else if ( ((hour > 21) && (hour <=23)) ||
13:                 ((hour >=0) && (hour <= 5)) ) {
14:         printf("Good night!\n");
15:     } else {
16:         printf("You entered the wrong input!\n");
17:     }

18:     printf("Have a good day!\n");
19:     return 0;
20: }

 

5

  1. After user input, control reaches line 06. Control expression (5>5 && 5<12) evaluates to false. (false && true is false). Control skips if block and jumps to else block, line 08.
  2. Control expression on line 08 (5>=12 && 5<=15) evaluates to false. Control skips if block and jumps to else block, line 10.
  3. Control expression on line 10 ( (5>21 && 5<=23) || (5>=0 && 5<=5) ) evaluates to true. ((false&&false) || (true&&true)) evaluates to (false || true) which evaluates to true. Control enters if block, line 14.
  4. Message Good night! is printed to the console. Control reaches end of if block, skips else block and jumps to line 18.
  5. Message Have a good day! is printed to the console. Control advances to end of program and exits.

 

12

  1. After user input, control reaches line 06. Control expression (12>5 && 12<12) evaluates to false. Control skips if block and jumps to else block, line 08.
  2. Control expression on line 08 (12>=12 && 12<=15) evaluates to true. Control enters if block, line 09.
  3. Message Good afternoon! is printed to the console. Control reaches end of if block, skips else block and jumps to line 18.
  4. Message Have a good day! is printed to the console. Control advances to end of program and exits.

 

7

  1. After user input, control reaches line 06. Control expression (7>5 && 7<12) evaluates to true. Control enters if block, line 07.
  2. Message Good morning! is printed to the console. Control reaches end of if block, skips else block and jumps to line 18.
  3. Message Have a good day! is printed to the console. Control advances to end of program and exits.

 

23

  1. After user input, control reaches line 06. Control expression (23>5 && 23<12) evaluates to false. Control skips if block and jumps to else block, line 08.
  2. Control expression on line 08 (23>=12 && 23<=15) evaluates to false. Control skips if block and jumps to else block, line 10.
  3. Control expression on line 10 ( (23>21 && 23<=23) || (23>=0 && 23<=5) ) evaluates to true. ((true&&true) || (false && false)) evaluates to (true || false) which evaluates to true. Control enters if block, line 14.
  4. Message Good night! is printed to the console. Control reaches end of if block, skips else block and jumps to line 18.
  5. Message Have a good day! is printed to the console. Control advances to end of program and exits.

 

15

  1. After user input, control reaches line 06. Control expression (15>5 && 15<12) evaluates to false. Control skips if block and jumps to else block, line 08.
  2. Control expression on line 08 (15>=12 && 15<=15) evaluates to true. Control enters if block, line 11.
  3. Message Good afternoon! is printed to the console. Control reaches end of if block, skips else block and jumps to line 18.
  4. Message Have a good day! is printed to the console. Control advances to end of program and exits.

 

 

12. Rewrite the grading program using if-else statements that decides various grades based on marks?

If marks >= 80 grade A

If marks < 80 and marks > 60 grade B

If marks < 60 and marks > 40 grade C

If marks < 40 grade F

#include <stdio.h>

int main (int argc, char * argv[]) {
    int marks = 0;
    printf("Enter marks scored (0-100): ");
    scanf("%d", &marks);

    if(marks > 100 || marks < 0) {
        printf("Invalid input\n");
    } else if (marks >= 80) {
        printf("Grade A\n");
    } else if (marks < 80 && marks >= 60) {
        printf("Grade B\n");
    } else if (marks < 60 && marks >= 40) {
        printf("Grade C\n");
    } else {
        printf("Grade F\n");
    }

    return 0;
}

 

13. Write a program that reads the month number (1-12) and prints its equivalent name (January for 1, February for 2, and so on). Include input error checking in your program.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int month = 0;

    printf("Enter month: ");
    scanf("%d", &month);

    if(month < 1 || month > 12) {
        printf("Invalid input\n");
    } else if(month == 1){
        printf("January\n");
    }else if(month == 2){
        printf("February\n");
    }else if(month == 3){
        printf("March\n");
    }else if(month == 4){
        printf("April\n");
    }else if(month == 5){
        printf("May\n");
    }else if(month == 6){
        printf("June\n");
    }else if(month == 7){
        printf("July\n");
    }else if(month == 8){
        printf("August\n");
    }else if(month == 9){
        printf("September\n");
    }else if(month == 10){
        printf("October\n");
    }else if(month == 11){
        printf("November\n");
    }else if(month == 12){
        printf("December\n");
    }

    return 0;
}

 

14. Trace the control flow of below program for following user inputs

month = 13 day = 5

month = 1 day = 9

month = 3 day = 4

month = 12 day = 7

month = 6 day = 7

month = 10 day = 2

01: #include <stdio.h>

02: int main (int argc, char * argv[]) {
03:     int month = 0, date = 0, day = 0;
04:     printf("Enter the current month (1-12): ");
05:     scanf("%d", &month);
06:     printf("Enter first day of month (1-7): ");
07:     scanf("%d", &day);

08:     if ( month < 1 || month > 12 ||
09:             day < 1 || day > 7) {
10:         printf("Invalid input\n");
11:     } else {
12:         if (2 == month) {
13:             printf("Month has 4 Sundays\n");
14:         } else {
15:             if ((1 == month|| 3 == month||
16:                     5 == month|| 7 == month ||
17:                     8 == month || 10 == month ||
18:                     12 == month) && (5 == day ||
19:                             6 == day || 7 == day)) {
20:                 printf("Month has 5 Sundays\n");
21:             }
22:         }
23:     }

24:     printf("Good Bye!\n");
25:     return 0;
26: }

 

month = 13 day = 5

  1. After user input, control reaches line 08. Control expression (13<1 || 13>12 || 5<1 || 5>7) evaluates to true. Control enters if block, line 10.
  2. Message Invalid input is printed to the console. Control reaches end of if block, skips the else block and jumps to line 24.
  3. Message Good Bye! is printed to the console. Control reaches end of program and terminates.

 

month = 1 day = 9

  1. After user input, control reaches line 08. Control expression (1<1 || 1>12 || 9<1 || 9>7) evaluates to true. Control enters if block, line 10.
  2. Message Invalid input is printed to the console. Control reaches end of if block, skips the else block and jumps to line 24.
  3. Message Good Bye! is printed to the console. Control reaches end of program and terminates.

 

month = 3 day = 4

  1. After user input, control reaches line 08. Control expression (3<1 || 3>12 || 4<1 || 4>7) evaluates to false. Control skips if block and jumps to else block, line 12.
  2. Control expression 2 == 3 is false. Control skips if block and jumps to else block, line 15.
  3. Control expression on line 15, ( (1==3 || 3==3 || 5==3 || 7==3 || 8==3 || 10==3 || 12==3) && (5==4 || 6==4 || 7 ==4)), evaluates to false. Control skips if block and jumps to line 22.
  4. Control reaches end of else block and advances to line 24.
  5. Message Good Bye! is printed to the console. Control reaches end of program and terminates.

 

month = 12 day = 7

  1. After user input, control reaches line 08. Control expression (12<1 || 12>12 || 7<1 || 7>7) evaluates to false. Control skips if block and jumps to else block, line 12.
  2. Control expression 2 == 12 is false. Control skips if block and jumps to else block, line 15.
  3. Control expression on line 15, ( (1==12 || 3==12 || 5==12 || 7==12 || 8==12 || 10==12 || 12==12) && (5==7 || 6==7 || 7 ==7)), evaluates to true. Control enters if block and advances to line 20.
  4. Message Month has 5 Sundays is printed to the console. Control advances to end of if block and advances to line 24.
  5. Message Good Bye! is printed to the console. Control reaches end of program and terminates.

 

month = 6 day = 7

  1. After user input, control reaches line 08. Control expression (6<1 || 6>12 || 7<1 || 7>7) evaluates to false. Control skips if block and jumps to else block, line 12.
  2. Control expression 2 == 6 is false. Control skips if block and jumps to else block, line 15.
  3. Control expression on line 15, ( (1==6 || 3==6 || 5==6 || 7==6 || 8==6 || 10==6 || 12==6) && (5==7 || 6==7 || 7 ==7)), evaluates to false. Control skips the if block and jumps to line 22.
  4. Control advances to line 24. Message Good Bye! is printed to the console. Control reaches end of program and terminates.

 

month = 10 day = 2

  1. After user input, control reaches line 08. Control expression (10<1 || 10>12 || 2<1 || 2>7) evaluates to false. Control skips if block and jumps to else block, line 12.
  2. Control expression 2 == 10 is false. Control skips if block and jumps to else block, line 15.
  3. Control expression on line 15, ( (1==10 || 3==10 || 5==10 || 7==10 || 8==10 || 10==10 || 12==10) && (5==2 || 6==2 || 7==2)), evaluates to false. Control skips the if block and jumps to line 22.
  4. Control advances to line 24. Message Good Bye! is printed to the console. Control reaches end of program and terminates.

 

 

15. Complete the above program to include calculations for all months and days

#include <stdio.h>

int main (int argc, char * argv[]) {
    int month = 0, date = 0, day = 0;

    printf("Enter the current month (1-12): ");
    scanf("%d", &month);
    printf("Enter first day of month (1-7): ");
    scanf("%d", &day);

    if ( month < 1 || month > 12 || day < 1 || day > 7) {
        printf("Invalid input\n");
    } else {
        if (2 == month) {
            printf("Month has 4 Sundays\n");
        } else {
            if ((1 == month|| 3 == month|| 5 == month|| 7 == month ||
                8 == month || 10 == month || 12 == month)) {
                if((5 == day || 6 == day || 7 == day)) {
                    printf("Month has 5 Sundays\n");
                } else {
                    printf("Month has 4 Sundays\n");
                }
            } else if (6 == day || 7 == day) {
                printf("Month has 5 Sundays\n");
            } else {
                printf("Month has 4 Sundays\n");
            }
        }
    }
    printf("Good Bye!\n");
    return 0;
}

 

16. Which of the following nested if else constructs are valid.

if ( … ) {
    if ( … ) {
        else { /* Invalid: This else has no if */
            …
        }
    }
}

 

if ( … ) {
    if ( … ) {
        if ( … ) {
            if ( … ) {
                …
            }
        } else { /* Valid: This else is attached to third if */
            …
        }
    }
}

 

if ( … ) {
    if ( … ) {
    } else { /* Valid: Attached to second if */
        …
    }
} else { /* Valid: Attached to first if */
    if ( … ) {
        …
    }
}

 

 

17. Write a calculator program that reads in two integers and an operator (+, -,  *, /) from the user and prints the result of applying that operator to the numbers. For example, if the user entered numbers 3 and 6 and operator -, your program should print -3 as result.

Question has been erroneously placed before introduction of char data type. Skip this question and come back to it after reading the chapter Data Types – The Sequel.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num1=0, num2=0;
    char operator=0;

    printf("Enter first operand: ");
    scanf("%d", &num1);
    printf("Enter second operand: ");
    scanf("%d", &num2);
    printf("Enter operator: ");
    scanf("%c", &operator);

    if('+' == operator) {
        printf("%d + %d = %d", num1, num2, num1+num2);
    } else if('-' == operator) {
        printf("%d - %d = %d", num1, num2, num1-num2);
    } else if('*' == operator) {
        printf("%d * %d = %d", num1, num2, num1*num2);
    } else if('/' == operator) {
        printf("%d / %d = %d", num1, num2, num1/num2);
    } else {
        printf("Invalid input");
    }
    return 0;
}

 

18. Write a program that reads the current hours and minutes from the user in GMT and converts them to current time IST. For doing so, you will have to add 5 hours 30 minutes to the entered time. If addition leads to change of day, just print the corresponding IST time for the following day. Make sure you validate the input values before using them.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int hour=0, mins=0, carry = 0;

    printf("Enter hour (0-23): ");
    scanf("%d", &hour);
    printf("Enter minutes (0-59): ");
    scanf("%d", &mins);

    if(hour<0 || hour>23 || mins<0 || mins>59) {
        printf("Invalid input\n");
    } else {
        if(mins+30 > 59) {
            carry = 1;
        }
        printf("Time in IST is %d:%d\n", (hour+5+carry)%24, (mins+30)%60);
    }
    return 0;
}

 

19. Run the above program with data type as double for f, sum and product. What is the magnitude of error you observe?

Ans: 

Not Applicable. Value will vary from system to system. Objective is to have the student discover imprecision on their own system.

 

20. What will be the output of following program.

#include <stdio.h>
int main(int argc, char* argv[]) {
    int a = 0;
    if (a == 0) {
        printf("The expression a == 0 is true\n");
    }
    if (a = 0) {
        printf("The expression a = 0 is true\n");
    }
    return 0;
}

Ans: a == 0 is an equality check that will evaluate to true.

a = 0 is an assignment operation that will evaluate to value assigned, i.e. 0. We know that 0 is false. Hence output will be

The expression a == 0 is true.