Question:

Consider the following C program

The value printed by the given C program is __________ (Answer in integer).

Show Hint

When processing numbers digit by digit in C, using modulo and division operations can help in extracting each digit in sequence. Alternating operations like this one can be useful to skip digits or reverse a sequence.
Updated On: Jan 30, 2026
Hide Solution
collegedunia
Verified By Collegedunia

Correct Answer: 46

Solution and Explanation

To determine the output of the given C program, we analyze the function gate with input 14362.

The function extracts every alternate digit starting from the second last digit, working from right to left. Here’s the detailed breakdown:

  1. Initialize: newnum=0, turn=0, t=1
  2. Calculate t as the smallest power of 10 greater than n. Initially, t=10
  3. In the while(t>0) loop, process each digit:
  • 1st Iteration: t=10000
    • d=n/t=1, n=4362, t=1000
    • turn=1
  • 2nd Iteration: t=1000
    • d=n/t=4, n=362, t=100
    • newnum=4 (since turn=1)
    • turn=0
  • 3rd Iteration: t=100
    • d=n/t=3, n=62, t=10
    • turn=1
  • 4th Iteration: t=10
    • d=n/t=6, n=2, t=1
    • newnum=46 (since turn=1)
    • turn=0
  • 5th Iteration: t=1
    • d=n/t=2, n=0,t=0
    • turn=1

Therefore, the function returns 46.

Finally, the main function prints this value:

printf("%d", gate(14362));

The output is: 46, which is within the given range [46,46].

Was this answer helpful?
0
0

Top Questions on Programming in C

View More Questions