Top 10+ C++ Interview Programs for 2024


 



Reverse a String

Explanation: This program reverses a given string.

Code:

#include <iostream> #include <string> #include <algorithm> int main() { std::string str; std::cout << "Enter a string: "; std::getline(std::cin, str); std::reverse(str.begin(), str.end()); std::cout << "Reversed string: " << str << std::endl; return 0; }


output:
Enter a string: hello Reversed string: olleh

Check Prime Number

Explanation: This program checks if a given number is prime.

Code:


#include <iostream> #include <cmath> bool isPrime(int num) { if (num <= 1) return false; for (int i = 2; i <= std::sqrt(num); i++) { if (num % i == 0) return false; } return true; } int main() { int num; std::cout << "Enter a number: "; std::cin >> num; if (isPrime(num)) { std::cout << num << " is a prime number." << std::endl; } else { std::cout << num << " is not a prime number." << std::endl; } return 0; }


output:
Enter a number: 29
29 is a prime number.


Sum of Array Elements

Explanation: This program calculates the sum of elements in an array.

Code:

#include <iostream> int main() { int n; std::cout << "Enter the number of elements: "; std::cin >> n; int arr[n], sum = 0; std::cout << "Enter the elements of the array:" << std::endl; for (int i = 0; i < n; i++) { std::cin >> arr[i]; sum += arr[i]; } std::cout << "The sum of the array elements is: " << sum << std::endl; return 0; }


output:
Enter the number of elements: 5 Enter the elements of the array: 1 2 3 4 5 The sum of the array elements is: 15


Factorial of a Number

Explanation: This program calculates the factorial of a given number.

Code:

#include <iostream> int factorial(int num) { if (num == 0 || num == 1) return 1; return num * factorial(num - 1); } int main() { int num; std::cout << "Enter a number: "; std::cin >> num; std::cout << "The factorial of " << num << " is: " << factorial(num) << std::endl; return 0; }

output:

Enter a number: 5 The factorial of 5 is: 120



Check Palindrome Number

Explanation: This program checks if a given number is a palindrome.

Code:

#include <iostream> bool isPalindrome(int num) { int original = num, reversed = 0; while (num != 0) { reversed = reversed * 10 + num % 10; num /= 10; } return original == reversed; } int main() { int num; std::cout << "Enter a number: "; std::cin >> num; if (isPalindrome(num)) { std::cout << num << " is a palindrome number." << std::endl; } else { std::cout << num << " is not a palindrome number." << std::endl; } return 0; }


output:
Enter a number: 121 121 is a palindrome number.

Fibonacci Series

Explanation: This program prints the Fibonacci series up to a given number of terms.

Code:


#include <iostream> void printFibonacci(int n) { int t1 = 0, t2 = 1, nextTerm = 0; for (int i = 1; i <= n; ++i) { if (i == 1) { std::cout << t1 << ", "; continue; } if (i == 2) { std::cout << t2 << ", "; continue; } nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; std::cout << nextTerm << ", "; } } int main() { int n; std::cout << "Enter the number of terms: "; std::cin >> n; std::cout << "Fibonacci Series: "; printFibonacci(n); std::cout << std::endl; return 0; }


output:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,



Armstrong Number

Explanation: This program checks if a given number is an Armstrong number.

Code:


#include <iostream> #include <cmath> bool isArmstrong(int num) { int original = num, sum = 0, digits = 0; while (original != 0) { original /= 10; digits++; } original = num; while (original != 0) { int remainder = original % 10; sum += std::pow(remainder, digits); original /= 10; } return sum == num; } int main() { int num; std::cout << "Enter a number: "; std::cin >> num; if (isArmstrong(num)) { std::cout << num << " is an Armstrong number." << std::endl; } else { std::cout << num << " is not an Armstrong number." << std::endl; } return 0; } output:
Enter a number: 153 153 is an Armstrong number.

Binary Search

Explanation: This program implements binary search on a sorted array.

Code:


#include <iostream> int binarySearch(int arr[], int size, int key) { int low = 0, high = size - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == key) return mid; if (arr[mid] < key) low = mid + 1; else high = mid - 1; } return -1; } int main() { int n, key; std::cout << "Enter the number of elements: "; std::cin >> n; int arr[n]; std::cout << "Enter the sorted elements of the array:" << std::endl; for (int i = 0; i < n; i++) { std::cin >> arr[i]; } std::cout << "Enter the key to search: "; std::cin >> key; int result = binarySearch(arr, n, key); if (result != -1) { std::cout << "Element found at index: " << result << std::endl; } else { std::cout << "Element not found." << std::endl; } return 0; } output:
Enter the number of elements: 5 Enter the sorted elements of the array: 1 3 5 7 9 Enter the key to search: 7 Element found at index: 3

 Insertion Sort

Explanation: This program implements insertion sort on an array.

Code:

#include <iostream> void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } int main() { int n; std::cout << "Enter the number of elements: "; std::cin >> n; int arr[n]; std::cout << "Enter the elements of the array:" << std::endl; for (int i = 0; i < n; i++) { std::cin >> arr[i]; } insertionSort(arr, n); std::cout << "Sorted array: "; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }


output:
Enter the number of elements: 5 Enter the elements of the array: 5 2






0 Comments:

Post a Comment