Hi dear C++ learners if you are wondering to find some helpful problems and their solution for your practice so you are reading the right post. In this post I have some problems which will enable you to implement the concepts of loops, switches, functions in C++.
Problem no.1:
Write a C++ program that allows the user to draw geometric shapes. The program should display a menu with the following options:
- Draw a right-angled triangle.
- Draw a rectangle.
- Draw a shallow right-angled triangle.
- Draw a shallow rectangle.
The program should prompt the user to enter their choice (1, 2, 3, or 4) and then follow these instructions based on their choice:
Option 1: If the user selects “1,” prompt them to enter the number of rows (a positive integer). Then, draw a right-angled triangle with the specified number of rows using asterisks (*) as shown below:

Option 2: If the user selects “2,” prompt them to enter the number of rows (a positive integer) and the number of columns (a positive integer). Then, draw a rectangle with the specified number of rows and columns using asterisks as shown below:

Option 3: If the user selects “3,” prompt them to enter the number of rows (a positive integer). Then, draw a shallow right-angled triangle with the specified number of rows using asterisks as shown below:

Option 4: If the user selects “4,” prompt them to enter the number of rows (a positive integer) and the number of columns (a positive integer). Then, draw a shallow rectangle with the specified number of rows and columns using asterisks as shown below:

- If the user enters an invalid choice or invalid input (non-positive integers), display an error message and allow them to try again.
Your program should continue to display the menu and execute the chosen option until the user decides to exit.
Solution:
#include<iostream>
using namespace std;
void menu() {
cout << “Enter 1 to draw a right-angled triangle” << endl;
cout << “Enter 2 to draw a rectangle” << endl;
cout << “Enter 3 to draw a shallow right-angled triangle” << endl;
cout << “Enter 4 to draw a shallow rectangle” << endl;
cout << “Enter 0 to exit” << endl;
cout << “Enter your choice: “;
}
void drawRightAngledTriangle(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << “* “;
}
cout << endl;
}
}
void drawRectangle(int rows, int cols) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
cout << “* “;
}
cout << endl;
}
}
void drawShallowRightAngledTriangle(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || i == rows || i == j) {
cout << “* “;
} else {
cout << ” “;
}
}
cout << endl;
}
}
void drawShallowRectangle(int rows, int cols) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (i == 1 || i == rows || j == 1 || j == cols) {
cout << “* “;
} else {
cout << ” “;
}
}
cout << endl;
}
}
int main() {
int choice;
while (true) {
menu();
cin >> choice;
if (choice == 0) {
break; // Exit the program
}
int rows, cols;
switch (choice) {
case 1:
cout << "Enter the number of rows: ";
cin >> rows;
drawRightAngledTriangle(rows);
break;
case 2:
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
drawRectangle(rows, cols);
break;
case 3:
cout << "Enter the number of rows: ";
cin >> rows;
drawShallowRightAngledTriangle(rows);
break;
case 4:
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
drawShallowRectangle(rows, cols);
break;
default:
cout << "Invalid Option. Please enter 1, 2, 3, 4, or 0 to exit." << endl;
break;
}
}
return 0;
}