Making decisions – if-else

1. Assuming int x = 5, y = 5 indicate which of the following expressions are true

1. x == y

Ans: True

2. x != y

Ans: False

3. x > y

Ans: False

4. x >= y

Ans: True

5. x < y

Ans: False

6. x <= y

Ans: True

 

2. What will be the output of below program for following user inputs

  1. -7
  2. 9
  3. 0
#include <stdio.h>

int main (int argc, char * argv[]) {
    int number = 0;
    printf("Enter a number: ");
    scanf("%d", &number);
    if (number < 0) {
        printf("You entered a negative number\n");
    }
    if (number > 0) {
        printf("You entered a positive number\n");
    }
    printf("Good Bye!\n");
    return 0;
}

1.

Check in line 07 evaluates to true and check on line 10 evaluates to false. Hence output is

You entered a negative number

Good Bye!

2.

Check in line 07 evaluates to false and check on line 10 evaluates to true. Hence output is

You entered a positive number

Good Bye!

3.

Check in line 07 evaluates to false and check on line 10 also evaluates to false. Hence output is

Good Bye!

 

3. Draw a flowchart for above program.

 

4. What will be the output of below program.

#include <stdio.h>

int main(int argc, char * argv[]) {
    int a = 100, b = 150, c = 200;

    if (a >= 100) {
        b = b + a;
        c = b + a;
    }

    if (c < 400) {
        a = 10;
    }

    printf ("%d %d %d\n", a, b, c);
}

Ans: if condition on line 06 evaluates to true, (100 >= 100).  Control enters if block

On line 07, values of a, b, and c become 100, 250, and 200 respectively.

On line 08, values of a, b, and c become 100, 250, and 300 respectively.

When control reaches line 11, value of c is 300. Hence if condition evaluates to true (300 < 400)

On line 12 value of a becomes 10 while b and c still have 250 and 300 respectively.

Hence, output printed is

10 250 350

 

5. What will be the output of below program for following user inputs

  1. 7
  2. 44
  3. 25
#include <stdio.h>

int main (int argc, char * argv[]) {
    float temperature = 0;
    printf("Enter current temperature (deg C): ");
    scanf("%f", &temperature);
    if (temperature < 12) {
        printf("It is getting cold.\n");
    }
    if (temperature > 35) {
        printf("It is hot!!\n");
    }
    printf("Good Bye!\n");
    return 0;
}

1.

if condition on line 07 evaluates to true (7 < 12) and condition on line 10 evaluates to false (7 > 35). Hence output printed is

It is getting cold.

Good Bye!

2.

if condition on line 07 evaluates to false (44 < 12) and condition on line 10 evaluates to true (44 > 35). Hence output printed is

It is hot!!

Good Bye!

3.

if condition on line 07 evaluates to false (25 < 12) and condition on line 10 also evaluates to false (25 > 35). Hence output printed is

Good Bye!

 

6. Draw a flowchart for above program.

 

7. Write a program that reads two numbers from the user and prints whether the numbers are equal or not.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num1 = 0, num2 = 0;
    printf("Enter first number ");
    scanf("%d", &num1);
    printf("Enter second number ");
    scanf("%d", &num2);

    if(num1 == num2) {
        printf("Numbers are equal\n");
    }
    if(num1 != num2) {
        printf("Numbers are not equal\n");
    }
    return 0;
}

 

8. Write a program that reads a number and prints whether the number is even or odd.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num = 0;
    printf("Enter a number ");
    scanf("%d", &num);
    if(num % 2 == 0) {
       printf("Number is even\n");
    }
    if(num % 2 != 0) {
       printf("Number is odd\n");
    }
    return 0;
}

 

9. Write a program that reads two numbers and prints if the first number is a factor of second number or not.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num1 = 0, num2 = 0;
    printf("Enter first number ");
    scanf("%d", &num1);
    printf("Enter second number ");
    scanf("%d", &num2);

    if(num1%num2 == 0) {
        printf("%d is a factor of %d\n", num2, num1);
    }
    if(num1%num2 != 0) {
        printf("%d is not a factor of %d\n", num2, num1);
    }
    return 0;
}

 

10. Write a program that reads two numbers and prints if the sum of two numbers is greater than 0, equal to 0, or less than 0.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num1 = 0, num2 = 0;
    printf("Enter first number ");
    scanf("%d", &num1);
    printf("Enter second number ");
    scanf("%d", &num2);

    if(num1 + num2 == 0) {
        printf("%d + %d = 0\n", num1, num2);
    }
    if(num1 + num2 > 0) {
        printf("%d + %d &gt; 0\n", num1, num2);
    }
    if(num1 + num2 < 0) {
        printf("%d + %d &lt; 0\n", num1, num2);
    }
    return 0;
}

 

11. Write a program that reads total taxable income and computes tax liability based on following slabs.

0 – 300,000 : 0%

300,000 – 600,000 : 10%

600,000 – 10,000,000 : 20%

10,00,001 – : 30%

#include <stdio.h>

int main (int argc, char * argv[]) {
    int income = 0, slab_income = 0;
    double tax = 0;
    printf("Enter taxable income ");
    scanf("%d", &income);
    /* First 300000 are tax free */
    income = income - 300000;

    /* Next up to 300000 are in 10% slab */
    if(income <= 300000 ) {
        slab_income = income;
    }
    if(income > 300000) {
        slab_income = 300000;
    }
    /* If input income was &lt; 300000 then slab_income would be negative
     * Hence we have to check to make sure we are not computing negative tax */
    if (slab_income > 0) {
        tax = slab_income * 0.1;
    }
    income = income - 300000;

    /* Next up to 400000 are in 20% slab */
    if(income <= 400000 ) {
        slab_income = income;
    }
    if(income > 400000) {
        slab_income = 400000;
    }
    /* slab_income would be negative due subtractions above
     * Hence we have to check to make sure we are not computing negative tax */
    if (slab_income > 0) {
        tax = tax + (slab_income * 0.2);
    }
    income = income - 400000;

    /* remaining income is in 30% slab */
    if(income > 0) {
        tax = tax + (income * 0.3);
    }

    printf("Your total tax is %f\n", tax);
    return 0;
}

 

12. Write a program that reads the month number (1-12) from the user and prints the name of the month (January, February, etc.). If the user input is not in the range 1 – 12, program should print an error message.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int month = 0;
    printf("Enter month number ");
    scanf("%d",&month);

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

    if(month < 1 ) {
        printf("Wrong input\n");
    }
    if(month > 12 ) {
        printf("Wrong input\n");
    }
    return 0;
}

 

13. Write a program that reads two numbers and prints the larger number.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num1 = 0, num2 = 0;
    printf("Enter first number ");
    scanf("%d",&num1);
    printf("Enter second number ");
    scanf("%d",&num2);

    if(num1 > num2) {
        printf("%d is larger\n", num1);
    }
    if(num2 > num1) {
        printf("%d is larger\n", num2);
    }
    return 0;
}

 

14. Write a program that reads three numbers and prints the largest number.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num1 = 0, num2 = 0, num3 = 0, larger = 0;
    printf("Enter first number ");
    scanf("%d",&num1);
    printf("Enter second number ");
    scanf("%d",&num2);
    printf("Enter third number ");
    scanf("%d",&num3);

    if(num1 >= num2) {
        larger = num1;
    }
    if(num2 >= num1) {
        larger = num2;
    }
    if(num3 > larger) {
        larger = num3;
    }

    printf("Largest number is %d\n",larger);
    return 0;
}

15. Which of the following if else constructs are syntactically valid.

1.

if (a>b) {
    a = a+2; 
} else {
    b = b+2;
}

Valid

 

2.

if (a>b)
    a = a+2;
else
    b = b+2;

Valid

 

3.

if (a>b)
    a = a+2;
else {
    b = b+2;
}

Valid

 

4.

if (a>b) {
   a = a+2;
} else
    b = b+2;

Valid

 

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

0

10

12

17

200

-9

 

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 < 12){
07:         printf("Its not noon yet.\n");
08:     } else {
09:         printf("Its past noon.\n");
10:     }
11:     printf("Have a good day!\n");
12:     return 0;
13: }

 

0

  1. After user input, control reaches line 06 with value 0 in hour.
  2. Control expression of if on line 06 is evaluated, 0 < 12 evaluates to true. Control enters if construct, line 07.
  3. Message Its not noon yet is printed to the console. Control skips else construct and jumps to line 11.
  4. Message Have a good day is printed to the console.
  5. Control advances to end of program and exits.

10

  1. After user input, control reaches line 06 with value 10 in hour.
  2. Control expression of if on line 06 is evaluated, 10 < 12 evaluates to true. Control enters if construct, line 07.
  3. Message Its not noon yet is printed to the console. Control skips else construct and jumps to line 11.
  4. Message Have a good day is printed to the console.
  5. Control advances to end of program and exits.

12

  1. After user input, control reaches line 06 with value 12 in hour.
  2. Control expression of if on line 06 is evaluated, 12 < 12 evaluates to false. Control jumps to else construct, line 09.
  3. Message Its past noon yet is printed to the console. Control advances to end of else construct line 11.
  4. Message Have a good day is printed to the console.
  5. Control advances to end of program and exits.

17

  1. After user input, control reaches line 06 with value 17 in hour.
  2. Control expression of if on line 06 is evaluated, 17 < 12 evaluates to false. Control jumps to else construct, line 09.
  3. Message Its past noon yet is printed to the console. Control advances to end of else construct line 11.
  4. Message Have a good day is printed to the console.
  5. Control advances to end of program and exits.

200

  1. After user input, control reaches line 06 with value 200 in hour.
  2. Control expression of if on line 06 is evaluated, 200 < 12 evaluates to false. Control jumps to else construct, line 09.
  3. Message Its past noon yet is printed to the console. Control advances to end of else construct line 11.
  4. Message Have a good day is printed to the console.
  5. Control advances to end of program and exits.

-9

  1. After user input, control reaches line 06 with value -9 in hour.
  2. Control expression of if on line 06 is evaluated, -9 < 12 evaluates to true. Control enters if construct, line 07.
  3. Message Its not noon yet is printed to the console. Control skips else construct and jumps to line 11.
  4. Message Have a good day is printed to the console.
  5. Control advances to end of program and exits.

 

17. Use if else to write a program that reads a non-zero number from the user and prints whether the number is positive or negative.

Note that we have not built any error checking into the program. If user enters 0 as input, we will end up printing number is negative. Adding check for invalid input is left as an exercise.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num = 0;
    printf("Enter a non zero number ");
    scanf("%d", &num);
    if(num > 0) {
        printf("Number is positive\n");
    } else {
        printf("Number is negative\n");
    }
    return 0;
}

 

18. Write a program that reads a number from the user and prints whether that number is a multiple of 7 or not.

#include <stdio.h>

int main (int argc, char * argv[]) {
    int num = 0;
    printf("Enter a number ");
    scanf("%d", &num);

    if(num%7 == 0) {
        printf("Number is multiple of 7\n");
    } else {
        printf("Number is not a multiple of 7\n");
    }
    return 0;
}

 

19. Modify the program for finding largest of three numbers to use if-else.

#include <stdio.h>

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

    printf("Enter first number ");
    scanf("%d",&num1);
    printf("Enter second number ");
    scanf("%d",&num2);
    printf("Enter third number ");
    scanf("%d",&num3);

    if(num1 >= num2) {
        larger = num1;
    } else {
        larger = num2;
    }
    if(num3 > larger) {
        larger = num3;
    }
    printf("Largest number is %d\n",larger);
    return 0;
}

 

20. What will be the output of following program.

#include <stdio.h>
int main (int argc, char * argv[]) {
    int a = 7, b = 9, c = 3, z = 0;
    if ((b + c) > z) {
        b = b * 2;
        z = b % a;
    } else {
        b = b / 2;
        z = a + b;
    }
    printf ("a=%d, b=%d, c=%d, z=%d\n", a, b, c, z);
    return 0;
}

Ans: Condition on line 04 evaluates to true (12 > 0). Control enters if block.

On line 05, value of b becomes 18. Values of a, b, c, d are now 7, 18, 3, and 0 respectively.

On line 06, value of z becomes 4. Values of a, b, c, d are now 7, 18, 3, and 4 respectively.

Else block is skipped, control jumps to line 11. Hence, following output is printed.

a=7, b=18, c=3, z=4

 

21. Which of the following code snippets are valid.

1.

int a = 5, b = 7, c = 11;
(a>b)?b=b+2;c=b*2:c=c*2;

Invalid. Entire ternary operator statement should evaluate to a single value.

 

2.

int a = 0, b = 0, c = 0;
a = (b+c > b-c)?b:c;

Valid.