Category: C++ Interview Questions
- 
		
		
		Write a Program to Print the Hourglass PatternC++// C Program to print hourglass pattern #include <iostream> using namespace std; // function to print hourglass pattern void hourglass(int rows) { // first outer loop to iterate each row for (int i = 0; i < 2 * rows – 1; i++) { // assigning comparator int comp; if (i < rows) { comp… 
- 
		
		
		Write a Program to Print a Pyramid Pattern* *** ***** ******* C++// C++ Program to // Print Pyramid pattern #include <iostream> using namespace std; void pattern(int n) { int k = 2 * n – 2; for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) cout << ” “; k = k… 
- 
		
		
		Write a Program to Print a Diamond Pattern* *** ***** ******* ***** *** * C++// C++ program to print // Diamond shape #include <iostream> using namespace std; void printDiamond(int n) { int space = n – 1; for (int i = 0; i < n; i++) { for (int j = 0; j < space; j++) cout << ” “; // Print… 
- 
		
		
		Write a Program to Check if Two Strings are Anagram or NotC++// C++ program to check if two strings // Are anagrams of each other #include <iostream> using namespace std; #define NO_OF_CHARS 256 bool areAnagram(char* str1, char* str2) { // Create 2 count arrays and initialize all values as 0 int count1[NO_OF_CHARS] = { 0 }; int count2[NO_OF_CHARS] = { 0 }; int i; // For… 
- 
		
		
		Write a Program to Check if the Given String is Palindrome or NotC++// C++ program for checking // if it is Palindrome or not #include <iostream> using namespace std; string isPalindrome(string S) { for (int i = 0; i < S.length() / 2; i++) { if (S[i] != S[S.length() – i – 1]) { return “No”; } } return “Yes”; } int main() { string S =… 
- 
		
		
		Write a Program to Calculate the Sum of Elements in an ArrayC++// C++ Program to calculate // sum of elements in an array #include <iostream> using namespace std; int sum(int arr[], int n) { int sum = 0; for (int i = 0; i < n; i++) sum += arr[i]; return sum; } int main() { int arr[] = { 1, 23, 54, 12, 9 };… 
- 
		
		
		Write a Program to Find the Second Smallest Element in an ArrayC++// C++ program to find // Second smallest elements #include <climits> #include <iostream> using namespace std; void print2Smallest(int arr[], int n) { int first, second; if (n < 2) { cout << ” Invalid Input “; return; } first = second = INT_MAX; for (int i = 0; i < n; i++) { // If… 
- 
		
		
		Write a Program to Find the Smallest and Largest Element in an ArrayC++// C++ code to for // Finding the minimum // And maximum of the array #include <iostream> using namespace std; // Function to find the minimum // and maximum of the array void findMinMax(int arr[], int n) { int mini = arr[0]; int maxi = arr[0]; for (int i = 0; i < n; i++)… 
- 
		
		
		Write a Program for Finding the Roots of a Quadratic EquationC++// C++ program to find // Roots of a quadratic equation #include <iostream> #include <math.h> using namespace std; // Prints roots of quadratic equation ax*2 + bx + c void findRoots(int a, int b, int c) { // If a is 0, then equation is not quadratic if (a == 0) { cout << “Invalid”;… 
- 
		
		
		Write a Program to Calculate the Lowest Common Multiple (LCM) of Two NumbersC++// C++ program to // Find LCM of two numbers #include <iostream> using namespace std; long long gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } // Function to return LCM of two numbers long long lcm(int a, int b) { long long…