Category: C++ Interview Questions

https://cdn3d.iconscout.com/3d/premium/thumb/interview-question-3d-icon-download-in-png-blend-fbx-gltf-file-formats–interviewer-hr-manager-pack-business-icons-9684875.png

  • Write a Program to Print the Hourglass Pattern

    * * * * * * * * *
    * * * * * * *
    * * * * *
    * * *
    *
    * * *
    * * * * *
    * * * * * * *
    * * * * * * * * *

    C++// 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 = 2 * i + 1; } else { comp = 2 * (2 * rows - i) - 3; } // first inner loop to print leading spaces for (int j = 0; j < comp; j++) { cout << ' '; } // second inner loop to print star * for (int k = 0; k < 2 * rows - comp; k++) { cout << "* "; } cout << '\n'; } } int main() { hourglass(5); return 0; }

    Output

     * * * * * * * * * 
       * * * * * * * 
    
     * * * * * 
       * * * 
         * 
       * * * 
     * * * * * 
    * * * * * * * * * * * * * * * *
  • 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 - 1; for (int j = 0; j <= i; j++) { // Printing stars cout << "* "; } cout << endl; } } int main() { int n = 5; pattern(n); return 0; }

    Output

            * 
    
       * * 
      * * * 
     * * * * 
    * * * * * </code></pre>
  • 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 i+1 stars for (int j = 0; j <= i; j++) cout << "* "; cout << endl; space--; } space = 0; // run loop (parent loop) for (int i = n; i > 0; i--) { for (int j = 0; j < space; j++) cout << " "; // Print i stars for (int j = 0; j < i; j++) cout << "* "; cout << endl; space++; } } int main() { printDiamond(5); return 0; }

    Output

        * 
       * * 
      * * * 
     * * * * 
    * * * * * 
    * * * * * 
     * * * * 
      * * * 
       * * 
    
    * </code></pre>
  • Write a Program to Check if Two Strings are Anagram or Not

    C++// 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 each character in input strings, increment count // in the corresponding count array for (i = 0; str1[i] && str2[i]; i++) { count1[str1[i]]++; count2[str2[i]]++; } if (str1[i] || str2[i]) return false; // Compare count arrays for (i = 0; i < NO_OF_CHARS; i++) if (count1[i] != count2[i]) return false; return true; } int main() { char str1[] = "Geek"; char str2[] = "for"; if (areAnagram(str1, str2)) cout << "The two strings are anagram of each other"; else cout << "The two strings are not anagram of each " "other"; return 0; }

    Output

    The two strings are not anagram of each other
  • Write a Program to Check if the Given String is Palindrome or Not

    C++// 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 = "GeekeeG"; cout << isPalindrome(S); return 0; }

    Output

    Yes
  • Write a Program to Calculate the Sum of Elements in an Array

    C++// 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 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Sum: " << sum(arr, n); return 0; }

    Output

    Sum: 99
  • Write a Program to Find the Second Smallest Element in an Array

    C++// 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 current element is smaller than first // Then update both first and second if (arr[i] < first) { second = first; first = arr[i]; } // If arr[i] is in between first and second // Then update second else if (arr[i] < second && arr[i] != first) second = arr[i]; } if (second == INT_MAX) cout << "There is no second smallest element\n"; else cout << " Second smallest element is " << second << endl; } int main() { int arr[] = { 21, 3, 15, 41, 34, 10 }; int n = sizeof(arr) / sizeof(arr[0]); print2Smallest(arr, n); return 0; }

    Output

     Second smallest element is 10
  •  Write a Program to Find the Smallest and Largest Element in an Array

    C++// 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++) { if (arr[i] < mini) { mini = arr[i]; } else if (arr[i] > maxi) { maxi = arr[i]; } } cout << "Min: " << mini << endl; cout << "Max: " << maxi << endl; } int main() { int arr[] = { 1, 2, 3, 4, 5 }; int N = sizeof(arr) / sizeof(arr[0]); findMinMax(arr, N); return 0; }

    Output

    Min: 1
    Max: 5
  • Write a Program for Finding the Roots of a Quadratic Equation

    C++// 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"; return; } // Formulae to calculate D int d = b * b - 4 * a * c; // Formulae to calculate // square root of D double sqrt_val = sqrt(abs(d)); // Conditons for checking root if (d > 0) { cout << "Roots are real and different \n"; cout << (double)(-b + sqrt_val) / (2 * a) << "\n" << (double)(-b - sqrt_val) / (2 * a); } else if (d == 0) { cout << "Roots are real and same \n"; cout << -(double)b / (2 * a); } else { cout << "Roots are complex \n"; cout << -(double)b / (2 * a) << " + i" << sqrt_val / (2 * a) << "\n" << -(double)b / (2 * a) << " - i" << sqrt_val / (2 * a); } } int main() { int a = 1, b = 4, c = 4; findRoots(a, b, c); return 0; }

    Output

    Roots are real and same 
    -2
  • Write a Program to Calculate the Lowest Common Multiple (LCM) of Two Numbers

    C++// 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 result = (a / gcd(a, b)) * b; return result; } int main() { int a = 24, b = 13; cout << "LCM : " << lcm(a, b); return 0; }

    Output

    LCM : 312