Ответ:
#include <iostream>
using namespace std;
void printSquare(int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << "* ";
}
cout << endl;
void printTriangle(int size) {
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= i; j++) {
void printDiamond(int size) {
int mid = size / 2;
for (int i = 0; i <= mid; i++) {
for (int j = 0; j < mid - i; j++) {
cout << " ";
for (int j = 0; j < 2 * i + 1; j++) {
for (int i = mid + 1; i < size; i++) {
for (int j = 0; j < i - mid; j++) {
for (int j = 0; j < 2 * (size - i) - 1; j++) {
int main() {
int choice, size;
while (true) {
cout << "Choose a figure to print:" << endl;
cout << "1. Square" << endl;
cout << "2. Triangle" << endl;
cout << "3. Diamond" << endl;
cout << "0. Exit" << endl;
cin >> choice;
if (choice == 0) {
break;
cout << "Enter the size of the figure: ";
cin >> size;
switch (choice) {
case 1:
printSquare(size);
case 2:
printTriangle(size);
case 3:
printDiamond(size);
default:
cout << "Invalid choice. Try again." << endl;
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Ответ:
#include <iostream>
using namespace std;
void printSquare(int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << "* ";
}
cout << endl;
}
}
void printTriangle(int size) {
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
}
void printDiamond(int size) {
int mid = size / 2;
for (int i = 0; i <= mid; i++) {
for (int j = 0; j < mid - i; j++) {
cout << " ";
}
for (int j = 0; j < 2 * i + 1; j++) {
cout << "* ";
}
cout << endl;
}
for (int i = mid + 1; i < size; i++) {
for (int j = 0; j < i - mid; j++) {
cout << " ";
}
for (int j = 0; j < 2 * (size - i) - 1; j++) {
cout << "* ";
}
cout << endl;
}
}
int main() {
int choice, size;
while (true) {
cout << "Choose a figure to print:" << endl;
cout << "1. Square" << endl;
cout << "2. Triangle" << endl;
cout << "3. Diamond" << endl;
cout << "0. Exit" << endl;
cin >> choice;
if (choice == 0) {
break;
}
cout << "Enter the size of the figure: ";
cin >> size;
switch (choice) {
case 1:
printSquare(size);
break;
case 2:
printTriangle(size);
break;
case 3:
printDiamond(size);
break;
default:
cout << "Invalid choice. Try again." << endl;
break;
}
}
return 0;
}