GATE CS 2014 Set 2 — Question 21
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+1 / -0.33MediumData Types & OperatorsC ProgrammingProgramming & Data Structures
Programming & Data Structures → C Programming → Data Types & Operators
Last updated
Question
Suppose
If
n and p are unsigned int variables in a C program. We wish to set p to . If
n is large, which one of the following statements is most likely to set p correctly?Correct answer
(B) p = n (n-1) / 2 (n-2) / 3;
Solution
We want to calculate .
Since
Since
n is large, computing n * (n-1) * (n-2) directly (as in option A) can cause an integer overflow before the division by 6 occurs.Option B: p = n * (n-1) / 2 * (n-2) / 3;1.
n * (n-1) is always even (product of two consecutive integers), so n * (n-1) / 2 is an integer and equals . This intermediate value is , which is much smaller than , reducing the risk of overflow.2.Then it multiplies by
Option C: (n-2) and divides by 3. Since the final result is an integer, the product of 3 consecutive integers is always divisible by . Thus, the intermediate value after multiplying by (n-2) will be divisible by 3.p = n * (n-1) / 3 ...n * (n-1) is not guaranteed to be divisible by 3 (e.g., if , is, but if , is not). Integer division would truncate the result, leading to an incorrect value.Option D uses floating point arithmetic (6.0), which might lose precision for large integers or result in a type mismatch warning/error depending on strictness, but the primary issue with large n in C is integer overflow, which B handles best.Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]