Category: C++ Interview Questions
-
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
-
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])…
-
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…
-
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
-
Write a Program to Print the Pascal Triangle
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…
-
Write a Program to Print Floyd’s Triangle
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;…
-
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 -…
-
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…
-
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 =…