C++ Interview Programs

Beginner
12 min read
Updated: Nov 2026
50 programs
Practice hub

Definition and Usage

C++ interview programs are short practice problems used in C++ tutorials, college labs, and fresher technical rounds. They reward clear loops, careful conditions, and clean iostream I/O more than large application design.

What you practice

Most drills build the same C++ skills: read input, process with for/while and if, print results, and handle edge cases. Number checks, digit math, arrays, matrices, and base conversions show up again and again—with extra attention to types, references, and safe resource habits.

What should beginners solve first?

Start with Even Number, Odd Number, and Prime Number. Then try Sum of Digits, Factorial, and Fibonacci Series before arrays and matrices.

This lesson vs program pages

This page is the C++ interview hub: how to approach problems, which topic to practice next, and how each tutorial is structured. The programs index lists 50 individual guides—each with algorithm, complete code, output, and FAQs.

Narrate before you type.

In interviews, say the inputs, the loop bounds, and the expected output first. Then write the C++ code.

Prefer RAII.

Use clear loops and std::vector when you need a dynamic array. Avoid manual new/delete unless asked.

Compile tip.

Most labs build with g++ file.cpp -std=c++17 -o out. Check that std::cin succeeded before using the value.

C++ Syntax

A typical interview program reads a value, decides with a condition or loop, and prints the result:

C++
#include <iostream>

int main() {
    int n;

    std::cout << "Enter a number: ";
    if (!(std::cin >> n)) {
        return 1;
    }

    if (n % 2 == 0) {
        std::cout << n << " is even.\n";
    } else {
        std::cout << n << " is odd.\n";
    }

    return 0;
}
/* g++ interview_skeleton.cpp -std=c++17 -o interview_skeleton */

Core C++ Concepts

Skills these C++ interview programs reinforce:

SkillWhat you practiceWhere it shows up
Core languageTypes, operators, if/else, loops, functions, namespacesEvery number drill
Streamsstd::cin, std::cout, fail checksInput/output in all labs
Arrays & vectorsC-style arrays for demos; std::vector when size variesSum / max / min of array
Pointers & referencesSwap helpers, array decay, pass-by-reference outputsSwap Two Numbers
Digit mathn % 10, n / 10 loopsArmstrong, palindrome, digit sum
MatricesNested loops over 2D arraysAddition, multiply, transpose
RecursionBase cases, stack depth, divide-and-conquerFactorial, Fibonacci variants
Base conversionRemainders and place valuesDecimal ↔ binary / octal
Memory habitsPrefer RAII containers over bare new/deleteAdvanced follow-ups

Learning order in this C++ tutorial

StageFocus
1. Number basicsEven, odd, prime, perfect, palindrome
2. OperationsDigit sum, GCD/LCM, factorial, Fibonacci
3. ArraysSum, maximum, minimum, average
4. Matrix & conversion2D ops and number-base drills

Choose a Topic by Goal

Pick a practice cluster based on what you need to strengthen:

GoalStart here
Warm up conditionsEven Number, Odd Number
Loop + divisor logicPrime Number, Perfect Number
Digit extractionSum of Digits, Armstrong Number
Series / recursion styleFactorial, Fibonacci Series
Array traversalSum of Array, Maximum of Array
2D thinkingMatrix Addition, Matrix Transpose

Jump to a category

How to Practice Interview Programs in C++

A practical workflow for this tutorial track:

  1. Read the problem: inputs, outputs, and constraints (range, negatives, zero).
  2. Write the algorithm in plain language before coding.
  3. Implement with #include <iostream>, clear names, and checked std::cin.
  4. Test edge cases, then compile: g++ file.cpp -std=c++17 -o out.
  5. Explain time complexity in one sentence (often O(1) or O(n) for these drills).
  6. Open the next program in the same category before jumping topics.

Live Preview

Enter an integer to preview the even/odd check—the same idea as Even Number and Odd Number.

Uses n % 2 == 0 for even, otherwise odd.

Preview
42 is even.

Interview tip: also mention 0 is even.

C++ Examples

Three starter patterns from this C++ tutorial track. Open the linked program page for the full walkthrough.

Example 1 — Even number check

The classic first interview warm-up in many C++ labs.

C++
#include <iostream>

int main() {
    int n = 18;

    if (n % 2 == 0) {
        std::cout << n << " is even.\n";
    } else {
        std::cout << n << " is odd.\n";
    }

    return 0;
}

How It Works

  1. % gives the remainder after division by 2.
  2. Remainder 0 means the number is even.
  3. std::cout reports the result clearly for the interviewer.

Example 2 — Sum of digits

Digit extraction with n % 10 and n / 10.

C++
#include <iostream>

int main() {
    int n = 482;
    int sum = 0;
    int temp = n;

    while (temp > 0) {
        sum += temp % 10;
        temp /= 10;
    }

    std::cout << "Sum of digits of " << n << " = " << sum << "\n";
    return 0;
}

How It Works

  1. temp % 10 peels off the last digit.
  2. temp /= 10 removes that digit.
  3. Loop until no digits remain.

Example 3 — Sum of an array

Fixed-array traversal—the bridge into matrix problems.

C++
#include <iostream>

int main() {
    int nums[] = { 4, 7, 1, 9 };
    int n = static_cast<int>(sizeof nums / sizeof nums[0]);
    int sum = 0;

    for (int i = 0; i < n; i++) {
        sum += nums[i];
    }

    std::cout << "Sum = " << sum << "\n";
    return 0;
}

How It Works

  1. sizeof finds the element count for a fixed array.
  2. for visits each index once.
  3. sum accumulates the total—O(n) time.

C++ Interview Programs Index

50 practice programs. Search by name or browse by category. Each row opens a full C++ tutorial.

Number Basics

20 programs

Classify and verify special integers—classic warm-up questions in C++ tutorials and campus rounds.

ProgramDescriptionTutorial
Even NumberCheck n % 2 == 0.Try it
Odd NumberCheck remainder 1.Try it
Natural NumberPositive integers.Try it
Composite NumberMore than two factors.Try it
Prime NumberOnly two factors.Try it
Perfect SquareSquare of an integer.Try it
Perfect NumberSum of proper divisors.Try it
Abundant NumberDivisor sum exceeds n.Try it
Strong NumberDigit factorial sum.Try it
Armstrong NumberPower sum of digits.Try it
Automorphic NumberSquare ends with n.Try it
Palindrome NumberReads the same reversed.Try it
Happy NumberDigit-square process.Try it
Harshad NumberDivisible by digit sum.Try it
Disarium NumberPositional power sum.Try it
Magic NumberRepeated digit sum is 1.Try it
Smith NumberDigit sum equals factor digits.Try it
Evil NumberEven count of 1-bits.Try it
Pronic NumberProduct of consecutives.Try it
Triangular Numbern(n+1)/2 form.Try it

Math and Operations

14 programs

Digit work, GCD/LCM, series, and everyday calculation drills.

ProgramDescriptionTutorial
Sum of DigitsAdd each digit of n.Try it
Condense a NumberReduce to a single digit.Try it
Swap Two NumbersExchange values safely.Try it
GCDGreatest common divisor.Try it
LCMLeast common multiple.Try it
Prime FactorFactorize into primes.Try it
Factorial of a Numbern! with a loop.Try it
Fibonacci SeriesGenerate the sequence.Try it
Leap YearCalendar leap rules.Try it
Power of 2Check 2^k form.Try it
Power of 3Check 3^k form.Try it
Cube NumberPerfect cubes.Try it
Number CombinationCombinations / nCr.Try it
Multiplication TablePrint a times table.Try it

Array Programs

6 programs

Traverse fixed arrays—sum, extremes, averages, and simple comparisons.

ProgramDescriptionTutorial
Sum of ArrayAdd all elements.Try it
Maximum Value of an ArrayFind the largest.Try it
Minimum Value of an ArrayFind the smallest.Try it
Average of N NumbersMean of N values.Try it
Biggest of Three NumbersCompare three ints.Try it
Common DivisorsShared divisors of two nums.Try it

Matrix and Conversion

10 programs

2D arrays, base conversions, and a few structured number problems.

ProgramDescriptionTutorial
Matrix AdditionAdd two matrices.Try it
Matrix SubtractionSubtract matrices.Try it
Matrix MultiplicationRow×column product.Try it
Matrix DivisionElement-wise division.Try it
Matrix TransposeSwap rows and columns.Try it
Decimal to BinaryBase-10 → base-2.Try it
Binary to DecimalBase-2 → base-10.Try it
Decimal to OctalBase-10 → base-8.Try it
Pascal's TriangleBinomial coefficients.Try it
Amicable NumberFriendly divisor pairs.Try it

Edge Cases & Interview Pitfalls

Call these out before the interviewer asks—it shows C++ tutorial maturity.

0

Zero and one

0 is even; 1 is not prime. Many number classifiers special-case small values.

-

Negatives

Ask whether the problem allows negatives. Digit loops often need std::abs or an early reject.

cin

Unchecked cin

Check that std::cin >> n succeeds before using the variable.

O(n)

Complexity one-liner

State big-O briefly: single-number checks are often O(√n) for primes; array scans are O(n).

Key Takeaways

  • Interview programs train loops, conditions, and iostream—not huge apps.
  • Start with even/odd, prime, digit sum, then arrays.
  • Explain first, then code—inputs, algorithm, edge cases, complexity.
  • Next: Even Number or browse the index above.

One line: Use this hub to pick a C++ interview drill, practice the pattern, then open the full program tutorial for code and FAQs.

Frequently Asked Questions

They are short C++ practice problems used in labs and fresher interviews—checking primes, digit sums, arrays, matrices, and number-base conversions. Each program trains loops, conditions, and iostream I/O more than large application design.
Start with Even Number, Odd Number, and Prime Number. Then try Sum of Digits, Factorial, and Fibonacci Series. Move to arrays (sum/max/min) before matrix and conversion drills.
State the inputs and expected output, outline the algorithm (loops and conditions), mention edge cases (0, negatives, empty arrays), then walk through the code. Compile with g++ and show a sample run when asked.
Fixed C-style arrays are fine for small demos. Prefer std::vector when size is unknown or you need a dynamic array. Interviewers often accept either if you state the trade-off.
Definition, algorithm, complete C++ code, sample output, walkthrough, and FAQs. Use this hub to pick a topic, then open the individual tutorial.
This hub is practice programs for interviews and labs. C++ Strings teaches std::string and related helpers. Use both tracks together when you need APIs plus problem drills.

Start with Even Number

Open the first warm-up tutorial—definition, algorithm, full code, and FAQs.

Even Number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful