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 Calculate the Length of the String Using Recursion

    C++// C++ Program for calculating // the length of string #include <iostream> using namespace std; int cal(char* str) { // base condition if (*str == '\0') return 0; else return 1 + cal(str + 1); } int main() { char str[] = "GeeksforGeeks"; cout << cal(str); return 0; }

    Output

    13
  • Write a Program to Check if the Given String is Palindrome or not Using Recursion

    C++// C++ program to check // Whether a given number // Is palindrome or not #include <bits/stdc++.h> using namespace std; bool isPalRec(char str[], int s, int n) { // If there is only one character if (s == n) return true; // If first and last // characters do not match if (str[s] != str[n]) return false; if (s < n + 1) return isPalRec(str, s + 1, n - 1); return true; } bool isPalindrome(char str[]) { int n = strlen(str); if (n == 0) return true; return isPalRec(str, 0, n - 1); } int main() { char str[] = "GeeKeeG"; if (isPalindrome(str)) cout << "Yes"; else cout << "No"; return 0; }

    Output

    Yes
  • Write a C++ Program to Print the Given String in Reverse Order Using Recursion

    C++// C++ Program to // Reverse string using // recursion #include <iostream> using namespace std; void reverse_str(string& s, int n, int i) { if (n <= i) { return; } swap(s[i], s[n]); reverse_str(s, n - 1, i + 1); } int main() { string str = "GeeksforGeeks"; reverse_str(str, str.length() - 1, 0); cout << str << endl; }

    Output

    skeeGrofskeeG
  • Write a Program to Print the Given String in Reverse Order 

    C++// C++ Program to reversea string #include <cstring> #include <iostream> using namespace std; int main() { int len; string str = "GeeksforGeeks"; len = str.size(); cout << "Reverse of the string: "; for (int i = len - 1; i >= 0; i--) { cout << str[i]; } cout << endl; return 0; }

    Output

    Reverse of the string: skeeGrofskeeG
  • Write a Program to Print the Pascal Triangle

                1   
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1

    C++// C++ program to print // Pascal’s Triangle #include <iostream> using namespace std; void printPascal(int n) { int arr[n][n]; for (int line = 0; line < n; line++) { // Every line has number of integers // equal to line number for (int i = 0; i <= line; i++) { // First and last values in every row are 1 if (line == i || i == 0) arr[line][i] = 1; else arr[line][i] = arr[line - 1][i - 1] + arr[line - 1][i]; cout << arr[line][i] << " "; } cout << "\n"; } } int main() { int n = 6; printPascal(n); return 0; }

    Output

    1 
    1 1 
    1 2 1 
    1 3 3 1 
    1 4 6 4 1 
    1 5 10 10 5 1 
  •  Write a Program to Print Floyd’s Triangle

    1
    2 3
    4 5 6
    7 8 9 10

    C++// C Program to print the Floyd's Triangle #include <stdio.h> int main() { int rows = 4; int n = 1; // outer loop to print all rows for (int i = 0; i < rows; i++) { // innter loop to print abphabet in each row for (int j = 0; j <= i; j++) { printf("%d ", n++); } printf("\n"); } return 0; }

    Output

    1 
    2 3 
    4 5 6 
    7 8 9 10 
  •  Write a Program to Print a Triangle Star Pattern

    C++// C++ Program to print a triangle star patter #include <iostream> using namespace std; int main() { int rows; rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { cout << "*"; } cout << endl; } return 0; }

    Output

    *
    **
    ***
    ****
    *****
  • Write a Program to print an Inverted Pyramid 

    C++// C++ Program to print inverted pyramid #include <iostream> using namespace std; int main() { int rows = 5; for (int i = rows; i >= 1; i--) { for (int j = rows; j > i; j--) { cout << " "; } for (int k = 1; k <= (2 * i - 1); k++) { cout << "*"; } cout << endl; } return 0; }

    Output

    *********
     *******
      *****
       ***
    
    *</code></pre>
  • Write a Program to Print a Simple Pyramid Pattern

    C++// C++ Program to print a simple pyramid #include <iostream> using namespace std; int main() { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = rows; j >= i; j--) { cout << " "; } for (int k = 1; k <= (2 * i - 1); k++) { cout << "*"; } cout << endl; } return 0; }

    Output

         *
    
    ***
    ***** ******* *********
  • Write a Program to Print the Rotated Hourglass Pattern

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

    C++// C++ Program to print // star pattern given #include <iostream> using namespace std; void pattern(int n) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { cout << "* "; } int spaces = 2 * (n - i); for (int j = 0; j < spaces; j++) { cout << " "; } for (int j = 0; j <= i; j++) { cout << "* "; } cout << endl; } // Printing bottom part. for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= i; j++) { cout << "* "; } int spaces = 2 * (n - i); for (int j = 0; j < spaces; j++) { cout << " "; } for (int j = 0; j <= i; j++) { cout << "* "; } cout << endl; } } int main() { int n = 5; pattern(n); return 0; }

    Output

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