GateAI
Programming in C

GATE CSE 2014 Set 2 · Q11

MCQ1 markMedium

Time

Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3^n\text{C}_3. If n is large, which one of the following statements is most likely to set p correctly?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) p = n * (n-1) / 2 * (n-2) / 3;

Programming in C

We need to compute:

nC3=n(n−1)(n−2)6^n\text{C}_3 = \frac{n(n-1)(n-2)}{6}

When nn is large, computing the product n(n−1)(n−2)n(n-1)(n-2) directly will easily cause integer overflow before the division by 6 is performed.

Let's evaluate each option:

  • (A)p = n * (n-1) * (n-2) / 6;: Computes the full 3-term product first, which is most vulnerable to integer overflow.

  • (B)p = n * (n-1) / 2 * (n-2) / 3;:

    • One of nn or n−1n-1 is always even, so n(n−1)n(n-1) is always divisible by 2. Thus, integer division n * (n-1) / 2 loses no precision and yields an exact integer.

    • The product of any three consecutive integers n(n−1)(n−2)n(n-1)(n-2) is divisible by 6. Since division by 2 was performed first without remainder, the remaining factor must be divisible by 3. Thus, ... * (n-2) / 3 also divides evenly with no fractional loss.

    • Performing the division early keeps intermediate values smaller, preventing overflow.

  • (C)p = n * (n-1) / 3 * (n-2) / 2;: n(n−1)n(n-1) is not necessarily divisible by 3 (for instance, if n=5n=5, 5×4=205 \times 4 = 20, and 20/3=620/3 = 6 truncates the fractional part), resulting in an incorrect answer.

  • (D)p = n * (n-1) * (n-2) / 6.0;: The integer expression n * (n-1) * (n-2) is still evaluated first in unsigned int arithmetic before promotion to double, so overflow still happens.

Therefore, (B) is the correct statement.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q11 of the GATE CSE 2014 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 20 of them are 1-mark MCQs like this one, and GATE CSE 2014 Set 2 has 2.

How GATE tests Programming in C

More from GATE CSE 2014 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 2 · Q11 · MCQ

Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which one of the following statements is most likely to set p correctly?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.