Placement Prep

C Programs Set 2: Six Placement-Round Problems Solved

Reverse a number, sum of digits, Armstrong check, GCD, Fibonacci, and prime with sqrt optimisation. Code, step traces, and time complexity for each.

By FACE Prep Team 7 min read
c-programming placement-prep coding-rounds algorithms number-theory placement-practice

Reverse, Fibonacci, prime, GCD, Armstrong, digit-sum: these six C programs appear in placement coding rounds with enough regularity that knowing them is the baseline, not the differentiator.

What separates candidates is being able to trace through the logic, state the time complexity without prompting, and handle a follow-up variation when the interviewer changes one constraint. Each section below covers all three: working code, a full step trace, and the complexity statement you should have ready.

This is Set 2. Set 1 of C coding questions covers array manipulation, string operations, and basic sorting. Worth studying alongside this set.

Why These Six Programs

C has ranked in the TIOBE Index top 3 consistently, which is one reason campus placement coding rounds still default to it as the entry-level language: it exposes memory and loop reasoning directly, without the runtime abstraction of Java or Python.

The six programs here share two properties. First, each is solvable in under 30 lines of idiomatic C. Second, each demonstrates a concept (digit extraction, number theory, series generation) that extends to harder problems in subsequent interview rounds. Knowing the pattern cold means a variation question costs you seconds rather than minutes.

One compile-time note: Program 6 (prime with sqrt) uses <math.h> and requires the -lm linker flag. Compile with gcc program.c -lm -o program. Every other program here uses only <stdio.h>.

Program 1: Reverse a Number

Problem: Read a positive integer and print its digits in reverse order.

#include <stdio.h>

int main() {
    int n, rev = 0, rem;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n != 0) {
        rem = n % 10;
        rev = rev * 10 + rem;
        n /= 10;
    }
    printf("Reversed number: %d\n", rev);
    return 0;
}

Step Trace (n = 153)

  • Start: n = 153, rev = 0
  • Iteration 1: rem = 3, rev = 3, n = 15
  • Iteration 2: rem = 5, rev = 35, n = 1
  • Iteration 3: rem = 1, rev = 351, n = 0
  • Loop ends. Output: 351

Sample I/O: Input 12345 produces Output 54321.

Time complexity: O(d), where d is the digit count. A 9-digit integer runs the loop at most 9 times.

Placement note: This digit-reversal pattern is the direct building block for palindrome checks. The same rem = n % 10 extraction recurs in Programs 2, 3, and 6 below. Expect a follow-up asking you to reverse a number without using a separate rev variable; that version uses recursion or a string buffer.

Program 2: Sum of Digits

Problem: Read a positive integer and print the sum of all its digits.

#include <stdio.h>

int main() {
    int n, sum = 0, rem;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n != 0) {
        rem = n % 10;
        sum += rem;
        n /= 10;
    }
    printf("Sum of digits: %d\n", sum);
    return 0;
}

Step Trace (n = 456)

  • Start: n = 456, sum = 0
  • Iteration 1: rem = 6, sum = 6, n = 45
  • Iteration 2: rem = 5, sum = 11, n = 4
  • Iteration 3: rem = 4, sum = 15, n = 0
  • Output: 15

Sample I/O: Input 9999 produces Output 36.

Time complexity: O(d). Structurally identical to Program 1: digit extraction loop, accumulate-by-addition instead of accumulate-by-shift.

Placement note: Digit sum feeds into divisibility checks. A number is divisible by 9 if and only if its digit sum is divisible by 9, and divisible by 3 if the digit sum is divisible by 3. Tests sometimes ask for these checks as a single combined program.

Program 3: Armstrong Number Check

Problem: Determine whether a given 3-digit number is an Armstrong number. A 3-digit Armstrong number satisfies: sum of cubes of its digits equals the number itself.

#include <stdio.h>

int main() {
    int n, original, rem, result = 0;
    printf("Enter a 3-digit number: ");
    scanf("%d", &n);
    original = n;
    while (n != 0) {
        rem = n % 10;
        result += rem * rem * rem;
        n /= 10;
    }
    if (result == original)
        printf("%d is an Armstrong number.\n", original);
    else
        printf("%d is not an Armstrong number.\n", original);
    return 0;
}

Step Trace (n = 153)

  • Start: n = 153, original = 153, result = 0
  • Iteration 1: rem = 3, result = 27, n = 15
  • Iteration 2: rem = 5, result = 152, n = 1
  • Iteration 3: rem = 1, result = 153, n = 0
  • 153 == 153: Armstrong number.

Sample I/O: Input 370 produces Output 370 is an Armstrong number.

Verification: 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370. Correct.

Time complexity: O(d). The 3-digit constraint means exactly 3 iterations.

Placement note: Interviewers sometimes extend the question to 4-digit Armstrong numbers (1634, 8208, 9474) by asking for the general n-digit case. For that version, count the digits first, then replace rem * rem * rem with pow(rem, digits) from <math.h>. The logic structure stays the same.

Program 4: GCD Using Euclidean Algorithm

Problem: Find the Greatest Common Divisor of two positive integers using the Euclidean algorithm.

#include <stdio.h>

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
    return 0;
}

Step Trace (a = 48, b = 18)

  • Iteration 1: temp = 18, b = 48 % 18 = 12, a = 18
  • Iteration 2: temp = 12, b = 18 % 12 = 6, a = 12
  • Iteration 3: temp = 6, b = 12 % 6 = 0, a = 6
  • b = 0, return a = 6

Sample I/O: Input 48 18 produces Output GCD of 48 and 18 is 6.

Time complexity: O(log(min(a, b))). Each iteration reduces the larger operand via the modulo operation, so convergence is rapid.

Placement note: LCM questions follow GCD almost immediately. Once you have gcd(a, b), LCM is (a * b) / gcd(a, b). Both fit in a single 10-minute coding slot. A recursive alternative for GCD: return b == 0 ? a : gcd(b, a % b). Worth having ready as a follow-up answer.

Program 5: Fibonacci Series up to N Terms

Problem: Print the first N terms of the Fibonacci series starting from 0.

#include <stdio.h>

int main() {
    int n, i, a = 0, b = 1, next;
    printf("Enter number of terms: ");
    scanf("%d", &n);
    printf("Fibonacci series: ");
    for (i = 1; i <= n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}

Step Trace (n = 6)

  • i = 1: print 0, next = 1, a = 1, b = 1
  • i = 2: print 1, next = 2, a = 1, b = 2
  • i = 3: print 1, next = 3, a = 2, b = 3
  • i = 4: print 2, next = 5, a = 3, b = 5
  • i = 5: print 3, next = 8, a = 5, b = 8
  • i = 6: print 5
  • Output: 0 1 1 2 3 5

Sample I/O: Input 8 produces Output 0 1 1 2 3 5 8 13.

Time complexity: O(N) time, O(1) space. This iterative version is preferred over the naive recursive version, which runs in exponential time without memoisation.

Placement note: The recursive form int fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } is fine to mention as an alternate approach, but state explicitly that its time complexity is O(2^N) without memoisation. Interviewers sometimes ask for both versions in the same slot.

Program 6: Prime Check Using Square Root Optimisation

Problem: Determine whether a given number is prime, using the square root boundary to limit iterations.

The optimisation rests on one property: if n is composite, it must have at least one factor at or below its square root. Checking only up to sqrt(n) is therefore sufficient. In practical terms:

  • Naive check (loop up to n): up to 1,000,000 iterations for n = 1,000,000
  • Halved (loop up to n/2): up to 500,000 iterations
  • Square root (loop up to sqrt(n)): up to 1,000 iterations
#include <stdio.h>
#include <math.h>

int main() {
    int n, i, flag = 0;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n <= 1) {
        printf("%d is not prime.\n", n);
        return 0;
    }
    for (i = 2; i <= (int)sqrt((double)n); i++) {
        if (n % i == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        printf("%d is prime.\n", n);
    else
        printf("%d is not prime.\n", n);
    return 0;
}

The cast (int)sqrt((double)n) is intentional. Per the cppreference.com documentation for sqrt, the function returns a double; casting to int prevents floating-point drift in the loop boundary comparison.

Step Trace (n = 37)

  • (int)sqrt(37) = 6 (since sqrt(37) is approximately 6.08)
  • i = 2: 37 % 2 = 1 (not divisible)
  • i = 3: 37 % 3 = 1
  • i = 4: 37 % 4 = 1
  • i = 5: 37 % 5 = 2
  • i = 6: 37 % 6 = 1
  • flag = 0. Output: 37 is prime.

Sample I/O: Input 36 produces Output 36 is not prime. (divisible by 2, so the loop exits at i = 2).

Time complexity: O(sqrt(n)). Contrast with O(n) for a brute-force check and O(N log log N) for a Sieve of Eratosthenes over a range of N numbers.

Placement note: For a follow-up asking you to print all primes up to N, the Sieve of Eratosthenes is the expected answer. The sieve is faster for batch generation than calling this function N times.

Time Complexity at a Glance

ProgramTimeSpace
Reverse a numberO(d)O(1)
Sum of digitsO(d)O(1)
Armstrong number (3-digit)O(d)O(1)
GCD, EuclideanO(log(min(a, b)))O(1)
Fibonacci (N terms, iterative)O(N)O(1)
Prime with sqrtO(sqrt(n))O(1)

Every program in this set runs in constant space. Stating that in an interview, without being asked, signals that you understand the difference between algorithmic efficiency and memory usage as separate axes.

For another pattern-based program that placement tests commonly include, Pascal’s triangle in C demonstrates nested loop control flow with a similar digit-logic structure.

The six programs above are the algorithm-recall layer of placement coding. The next layer that companies increasingly include involves building something functional under similar time pressure. TinkerLLM is where that shift starts: at ₹299, it gives you real LLM API calls and project prompts drawn from the same build-under-constraints discipline, so the skills from these programs carry forward rather than stopping at the coding round.

Primary sources

Frequently asked questions

What is an Armstrong number in C programming?

An Armstrong number is one where the sum of its digits, each raised to the power of the total digit count, equals the number itself. For 3-digit numbers: 153 equals 1 cubed plus 5 cubed plus 3 cubed, which is 153.

Why use sqrt() in a prime number check instead of checking up to n/2?

If n has a factor larger than its square root, it must also have a corresponding factor smaller than the square root. Checking up to sqrt(n) is sufficient and cuts the iteration count from roughly n/2 to sqrt(n), which is much faster for large inputs.

What is the time complexity of GCD using the Euclidean algorithm?

The Euclidean algorithm runs in O(log(min(a, b))) time. Each iteration reduces the problem size because the remainder is always smaller than both operands, so convergence is rapid even for very large inputs.

How does the Fibonacci program print N terms in C without recursion?

Initialise a = 0 and b = 1. In a for loop from 1 to N, print a, then compute next = a + b, set a = b, and set b = next. This iterative approach uses O(1) space and O(N) time.

Can I use recursion instead of a loop for GCD?

Yes. The recursive form is: return b == 0 ? a : gcd(b, a % b). Both iterative and recursive versions implement the same Euclidean logic and are accepted in placement coding tests. The iterative version avoids stack-overflow risk for very large inputs.

Which programs from this set appear most often in placement coding rounds?

Reverse a number, Fibonacci series, and prime checking appear across the widest range of entry-level placement coding assessments. GCD and Armstrong checks surface in tests that include a number-theory or mathematics section.

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