Placement Prep

5 C Programs Asked in Placement Tests: Factorial, Even/Odd, Swap

Factorial, even/odd check, and three swap methods: five C programs that appear across TCS, Infosys, and aptitude platforms. Annotated code with complexity notes.

By FACE Prep Team 6 min read
c-programming placement-prep algorithms coding-interview data-structures

Five C programs account for a large share of the basic coding questions in placement aptitude tests: factorial, even/odd check, and three variations of the swap problem.

These aren’t trick questions. They test whether a student can write correct code quickly, state the edge cases without prompting, and explain what the code does, not just what it outputs. That combination of speed, correctness, and articulation is what separates the shortlisted candidates.

Why These Programs Appear in Placement Rounds

Placement coding rounds at TCS NQT, Infosys InfyTQ, and the AMCAT Automata module share a pattern: they start with small, well-defined problems that are easy to verify automatically. Factorial, even/odd, and swap all fit that shape: the expected output is deterministic, the solution space is small, and a wrong answer is immediately checkable.

That said, each program hides a trap:

  • Factorial: integer overflow for large inputs.
  • Even/odd: behaviour with negative numbers.
  • Swap: aliasing failure in the XOR variant.

Interviewers extend these programs to probe depth. The student who writes a factorial loop and stops gets the marks. The student who adds the overflow guard and mentions unsigned long long gets the follow-up conversation. FACE Prep has observed this pattern across placement cycles at Tier-2 colleges in Tamil Nadu, Andhra Pradesh, and Karnataka. The programs appear simple, but the score spread comes from the edge-case discussion.

For the full range of coding patterns that appear in these tests, see data structures interview questions.

Factorial of a Number

The Problem

Given a non-negative integer n, compute n factorial (written as n!), defined as the product of all positive integers from 1 to n. By convention, 0! equals 1.

C Code

#include <stdio.h>

int main() {
    int n, i;
    unsigned long long factorial = 1;

    printf("Enter a non-negative integer: ");
    scanf("%d", &n);

    if (n < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        for (i = 1; i <= n; i++) {
            factorial *= i;
        }
        printf("Factorial of %d = %llu\n", n, factorial);
    }
    return 0;
}

Key Points

  • Why unsigned long long: int holds values up to 2,147,483,647. 12! equals 479,001,600 (fits in int), but 13! equals 6,227,020,800 (overflows). Declaring factorial as unsigned long long covers inputs up to 20 safely.
  • Negative-input guard: the if (n < 0) branch handles the edge case explicitly. Placement tests frequently give negative inputs as test cases to check whether the program handles them.
  • Loop bounds: the loop runs from 1 to n inclusive. For n = 0, the loop body never executes and factorial stays at its initial value of 1, which is the correct result.
  • Format specifier: %llu is the correct format specifier for unsigned long long in printf. Per the cppreference printf reference, using %d on an unsigned long long argument is undefined behaviour — always match the specifier to the declared type.

The C arithmetic operators reference documents how *= applies the multiplication-assignment, which is what the loop body uses on each iteration.

Even or Odd Check

The Problem

Given an integer, determine whether it is even or odd.

C Code

#include <stdio.h>

int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    if (number % 2 == 0)
        printf("%d is even.\n", number);
    else
        printf("%d is odd.\n", number);

    return 0;
}

Key Points

  • How it works: the % operator returns the remainder after integer division. Any even number divided by 2 leaves a remainder of 0; any odd number leaves a non-zero remainder.
  • Negative numbers: C’s modulo returns a result with the same sign as the dividend. For -7, the result is -1. The check == 0 still correctly identifies evens because any negative even number still yields 0.
  • Bitwise alternative: number & 1 checks the least-significant bit directly. If it is 1, the number is odd. This works on any two’s-complement system and avoids the division. Either approach is acceptable in placement coding rounds.

When to Mention the Bitwise Alternative

Write the modulo version first. It is more readable and is what every interviewer expects to see. Mention & 1 as an alternative if the interviewer asks about performance, but clarify that modern compilers often emit the same machine code for both forms anyway.

Three Ways to Swap Two Numbers

Method 1: Temporary Variable

#include <stdio.h>

int main() {
    int a, b, temp;
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    temp = a;
    a = b;
    b = temp;

    printf("After swap: a = %d, b = %d\n", a, b);
    return 0;
}

Three assignments, no arithmetic, no aliasing concern. This is the method to write first in any context.

Method 2: Arithmetic (No Temp Variable)

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter two integers: ");
    scanf("%d %d", &a, &b);

    b = a + b;
    a = b - a;
    b = b - a;

    printf("After swap: a = %d, b = %d\n", a, b);
    return 0;
}

Trace with a = 3, b = 5:

  • Step 1: b = a + b gives b = 8
  • Step 2: a = b - a gives a = 8 - 3 = 5
  • Step 3: b = b - a gives b = 8 - 5 = 3

Limitation: integer overflow when a + b exceeds INT_MAX. For placement tests with small inputs, this is rarely triggered, but it is worth mentioning if the interviewer asks.

Method 3: Bitwise XOR

#include <stdio.h>

void swap(int *x, int *y) {
    *x = *x ^ *y;
    *y = *x ^ *y;
    *x = *x ^ *y;
}

int main() {
    int a = 3, b = 5;
    swap(&a, &b);
    printf("After swap: a = %d, b = %d\n", a, b);
    return 0;
}

Trace with a = 3 (binary 011), b = 5 (binary 101):

  • After *x = *x ^ *y: x = 011 ^ 101 = 110 (6)
  • After *y = *x ^ *y: y = 110 ^ 101 = 011 (3)
  • After *x = *x ^ *y: x = 110 ^ 011 = 101 (5)

Critical aliasing limitation: if x and y point to the same address, the first XOR sets the variable to zero. All subsequent XOR operations on zero return zero, so both variables end up as zero. This is the most important thing to state when presenting this method.

Comparison Table

MethodVariables usedOverflow riskAliasing safeRecommended for
Temp variable3NoYesAll contexts
Arithmetic2Yes (large inputs)YesExplaining trade-offs
Bitwise XOR2NoNoInterview depth discussion

Where These Patterns Show Up Across Tests

Each of these five programs appears in placement coding rounds in a specific format:

  • Factorial appears as a standalone problem in basic coding sections of TCS NQT Digital and Infosys InfyTQ. The overflow edge case is a common verbal follow-up.
  • Even/odd appears in multiple-choice format in aptitude sections and as an output-based question (“what does this code print for input -7?”).
  • Swap variants appear as a trio in technical interview rounds. Interviewers hand a candidate all three methods and ask which one they would use in production code and why.

The pattern transfers to other problems in the same family. Array traversal (walking an array once and updating a running result) is exactly how finding the smallest and largest element in an array works, and how the swap logic generalises to in-place array reversals. The palindrome check uses a similar two-pointer traversal applied to characters.

Placement coding rounds test these programs because they are the vocabulary of procedural thinking. Getting them right means stating the edge cases, putting the overflow guard in place, and noting the XOR aliasing caveat. That combination moves a student from the pass/fail bucket into the interview conversation bucket.

The same loop-and-compare structure that powers these programs also appears inside AI inference pipelines: a model scores candidates and tracks a running maximum score across outputs, using the identical update pattern. TinkerLLM at ₹299 connects the procedural logic from programs like these to how LLM evaluation and ranking actually work. The gap from placement prep to applied AI is shorter than it looks.

Primary sources

Frequently asked questions

Why does the factorial program use unsigned long long?

A plain int holds values up to 2,147,483,647. 13 factorial is 6,227,020,800 — larger than that limit. unsigned long long extends the range to 18,446,744,073,709,551,615, which covers factorial values up to 20 without overflow.

Can the XOR swap fail for any input?

Yes. If both pointers point to the same memory address, the first XOR sets the value to zero. Every subsequent XOR operates on zero, leaving the variable at zero rather than restoring the original value. Always use the temp-variable swap unless a separate-address guarantee is enforced.

Which swap method should I write in a placement coding test?

Write the temp-variable method first. It is always correct, immediately readable, and what interviewers expect as the baseline. Mention the XOR variant as an alternative, state its aliasing limitation, and let the interviewer decide if they want to explore it.

Does the even/odd check work for negative numbers?

Yes. The modulo operator in C returns a result with the same sign as the dividend, so -7 % 2 returns -1, not 1. The check n % 2 == 0 is still reliable for even/odd because any even number gives 0 and any odd number gives a non-zero result regardless of sign.

Are these programs tested in C or C++?

Most placement platforms accept both. TCS NQT, Infosys InfyTQ, and AMCAT Automata all support C and C++ in their coding modules. The programs in this article are written in C for clarity, but each translates directly to C++ with cin/cout replacing scanf/printf.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF