ให้ด้วยค่าบวก n และงานคือการสร้างรูปแบบสามเหลี่ยมเช่นภาพสะท้อนของตัวเลขที่พิมพ์และแสดงผล
ตัวอย่าง
Input-: n = 6 Output-:

Input-: n = 3 Output-:

แนวทางที่ใช้ในโปรแกรมด้านล่างมีดังนี้ −
- ป้อนค่าของ n เป็นจำนวนเต็มบวก
- ข้ามหนึ่งวง i เพื่อหาจำนวนแถวในรูปแบบเช่น n
- ข้าม 1 วง j เพื่อหาจำนวนช่องว่างในรูปแบบ
- ข้ามไปอีกลูปสำหรับตัวเลขในรูปแบบ
อัลกอริทึม
START Step 1-> declare function to print mirror image of triangular pattern void print_mirror(int n) declare and set int temp = 1 and temp2 = 1 Loop for int i = 0 and i < n and i++ Loop For int j = n - 1 and j > i and j— print space End Loop For int k = 1 and k <= temp and k++ print abs(k - temp2) End Set temp += 2 increment temp2++ print \n Step 2-> In main() Declare int n = 6 print_mirror(n) STOP
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//function to print mirror image of triangular pattern
void print_mirror(int n) {
int temp = 1, temp2 = 1;
for (int i = 0; i < n; i++) {
for (int j = n - 1; j > i; j--) {
cout << " ";
}
for (int k = 1; k <= temp; k++) {
cout << abs(k - temp2);
}
temp += 2;
temp2++;
cout << "\n";
}
}
int main() {
int n = 6;
print_mirror(n);
return 0;
} ผลลัพธ์
