In C programming, what will be the output of the following program that computes the average of a maximum and minimum value? #include <stdio.h> int main() { int max_val = 100; int min_val = 10; int avg_val; avg_val = (max_val + min_val) / 2; printf("%d", avg_val); return 0; }

Difficulty: Easy

Correct Answer: 55

Explanation:


Introduction / Context:
This question checks your ability to read a simple C program and evaluate an arithmetic expression. It also reinforces the idea that integer division in C discards any fractional part, which often appears in basic programming and aptitude tests on C.


Given Data / Assumptions:

    • max_val is initialized to 100.

    • min_val is initialized to 10.

    • avg_val is assigned the expression (max_val + min_val) / 2.

    • The program prints avg_val using "%d".

    • We assume the identifiers use underscores (max_val, min_val, avg_val) so that the program compiles correctly.



Concept / Approach:
To find the average of two integer values, you add them and then divide the sum by 2. In this particular example, both operands are integers, so normal integer arithmetic applies. The sum of 100 and 10 is 110, and dividing 110 by 2 yields 55 exactly, so there is no fractional part to be discarded. The printf call then simply prints 55 as a decimal integer on the screen.


Step-by-Step Solution:
Step 1: Compute max_val + min_val: 100 + 10 = 110. Step 2: Compute (max_val + min_val) / 2 as integer division: 110 / 2 = 55. Step 3: Assign this result to avg_val, so avg_val becomes 55. Step 4: Execute printf("%d", avg_val); which prints the integer value stored in avg_val, namely 55, without a newline. Step 5: The program returns 0 from main and terminates normally after printing 55.


Verification / Alternative check:
You can cross-check by using the general formula average = (max + min) / 2 for any pair of numbers. If max_val were 50 and min_val were 30, the average would be (50 + 30) / 2 = 40, which matches what you would compute mathematically. For 100 and 10, the same pattern gives (100 + 10) / 2 = 55. No rounding issues appear here because the sum is evenly divisible by 2.


Why Other Options Are Wrong:
Option B (105) incorrectly treats the operation as addition only, ignoring the division by 2. Option C (60) may come from incorrectly averaging by taking (100 + 20) / 2 or some other mistaken combination, but it does not match the given values. Option D (Compilation error) would only be correct if the variable names were illegal or headers were missing; here we assume the corrected code with proper identifiers, which compiles without errors.


Common Pitfalls:
One pitfall in similar questions is forgetting that integer division truncates towards zero when there is a fractional part. For example, (5 + 6) / 2 would yield 5 in integer arithmetic, not 5.5. Another mistake is to misread the variable names or miscompute the sum. Carefully following the order of operations and checking values step by step helps avoid such errors.


Final Answer:
The program computes the average as (100 + 10) / 2 = 55, so the output is 55.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion