C++ Practical Coding Interview Questions and Answers (With Output Examples) – 2025 Updated

Mou

October 30, 2025

In most C++ interviews, companies not only ask theory questions but also test your coding skills.
These C++ practical coding interview questions help you understand real logic, problem-solving, and syntax — all explained in simple and easy words for beginners.


1. Write a program to find the largest of three numbers.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;

    if (a >= b && a >= c)
        cout << "Largest number is: " << a;
    else if (b >= a && b >= c)
        cout << "Largest number is: " << b;
    else
        cout << "Largest number is: " << c;

    return 0;
}

Output:

Enter three numbers: 10 25 8  
Largest number is: 25

2. Write a program to check if a number is even or odd.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if (num % 2 == 0)
        cout << num << " is Even.";
    else
        cout << num << " is Odd.";

    return 0;
}

Output:

Enter a number: 13  
13 is Odd.

3. Program to check if a number is prime.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n, i, flag = 0;
    cout << "Enter a number: ";
    cin >> n;

    if (n <= 1) flag = 1;
    for (i = 2; i <= n / 2; i++) {
        if (n % i == 0) {
            flag = 1;
            break;
        }
    }

    if (flag == 0)
        cout << n << " is a Prime Number.";
    else
        cout << n << " is Not a Prime Number.";
}

Output:

Enter a number: 11  
11 is a Prime Number.

4. Write a program to find factorial of a number.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n;
    long long fact = 1;
    cout << "Enter a number: ";
    cin >> n;

    for (int i = 1; i <= n; i++)
        fact *= i;

    cout << "Factorial = " << fact;
}

Output:

Enter a number: 5  
Factorial = 120

5. Write a program to reverse a number.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n, rev = 0, rem;
    cout << "Enter a number: ";
    cin >> n;

    while (n != 0) {
        rem = n % 10;
        rev = rev * 10 + rem;
        n /= 10;
    }

    cout << "Reversed Number = " << rev;
}

Output:

Enter a number: 1234  
Reversed Number = 4321

6. Program to check palindrome number.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n, rev = 0, rem, temp;
    cout << "Enter a number: ";
    cin >> n;
    temp = n;

    while (n != 0) {
        rem = n % 10;
        rev = rev * 10 + rem;
        n /= 10;
    }

    if (temp == rev)
        cout << "Palindrome Number";
    else
        cout << "Not a Palindrome";
}

Output:

Enter a number: 121  
Palindrome Number

7. Write a program to print Fibonacci series.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n, a = 0, b = 1, next;
    cout << "Enter number of terms: ";
    cin >> n;

    cout << "Fibonacci Series: ";
    for (int i = 1; i <= n; i++) {
        cout << a << " ";
        next = a + b;
        a = b;
        b = next;
    }
}

Output:

Enter number of terms: 6  
Fibonacci Series: 0 1 1 2 3 5

8. Write a program to swap two numbers without using a third variable.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;

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

    cout << "After swapping: a = " << a << ", b = " << b;
}

Output:

Enter two numbers: 5 8  
After swapping: a = 8, b = 5

9. Write a program to find the sum of digits of a number.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    cout << "Enter a number: ";
    cin >> n;

    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }

    cout << "Sum of digits = " << sum;
}

Output:

Enter a number: 1234  
Sum of digits = 10

10. Write a program to find whether a string is a palindrome.

Example Code:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str, rev;
    cout << "Enter a string: ";
    cin >> str;

    rev = string(str.rbegin(), str.rend());

    if (str == rev)
        cout << "Palindrome String";
    else
        cout << "Not a Palindrome";
}

Output:

Enter a string: madam  
Palindrome String

11. Write a program to count vowels and consonants in a string.

Example Code:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    int v = 0, c = 0;
    cout << "Enter a string: ";
    getline(cin, str);

    for (char ch : str) {
        ch = tolower(ch);
        if (ch >= 'a' && ch <= 'z') {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                v++;
            else
                c++;
        }
    }

    cout << "Vowels: " << v << ", Consonants: " << c;
}

Output:

Enter a string: Hello World  
Vowels: 3, Consonants: 7

12. Write a program to find the sum of array elements.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    cout << "Enter number of elements: ";
    cin >> n;

    int arr[n];
    cout << "Enter elements: ";
    for (int i = 0; i < n; i++) cin >> arr[i];

    for (int i = 0; i < n; i++) sum += arr[i];

    cout << "Sum = " << sum;
}

Output:

Enter number of elements: 4  
Enter elements: 10 20 30 40  
Sum = 100

13. Write a program to sort an array in ascending order.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of elements: ";
    cin >> n;

    int arr[n];
    cout << "Enter elements: ";
    for (int i = 0; i < n; i++) cin >> arr[i];

    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] > arr[j])
                swap(arr[i], arr[j]);
        }
    }

    cout << "Sorted array: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}

Output:

Enter number of elements: 5  
Enter elements: 9 2 4 1 5  
Sorted array: 1 2 4 5 9

14. Write a program to find the second largest element in an array.

Example Code:

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter size: ";
    cin >> n;
    int arr[n];

    for (int i = 0; i < n; i++)
        cin >> arr[i];

    int first = arr[0], second = -1;

    for (int i = 1; i < n; i++) {
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        } else if (arr[i] > second && arr[i] != first) {
            second = arr[i];
        }
    }

    cout << "Second largest element: " << second;
}

Output:

Enter size: 5  
10 20 8 15 30  
Second largest element: 20

15. Write a program to count the frequency of each character in a string.

Example Code:

#include <iostream>
#include <map>
using namespace std;

int main() {
    string str;
    cout << "Enter a string: ";
    cin >> str;

    map<char, int> freq;

    for (char ch : str)
        freq[ch]++;

    for (auto it : freq)
        cout << it.first << " : " << it.second << endl;
}

Output:

Enter a string: apple  
a : 1  
e : 1  
l : 1  
p : 2

Final Tips for C++ Coding Interviews

  • Practice small programs daily — logic matters most.
  • Focus on loops, arrays, and strings — these are asked very often.
  • Revise STL (vectors, maps) — companies love these.
  • Don’t just memorize; understand the logic behind each problem.

Leave a Comment