ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมพิมพ์ตัวอักษร 'N' โดยใช้รูปแบบตัวเลขตั้งแต่ 1 ถึง n
สำหรับสิ่งนี้เราจะต้องพิมพ์ตัวอักษรภาษาอังกฤษ N หน้าที่ของเราคือกำหนดขนาดของตัวอักษรและพิมพ์กลับโดยใช้ตัวเลขตั้งแต่ 1 ถึง n
ตัวอย่าง
#include <iostream>
using namespace std;
//printing the letter N
void print_N(int N){
int index, side_index, size;
int Right = 1, Left = 1, Diagonal = 2;
for (index = 0; index < N; index++) {
cout << Left++;
for (side_index = 0; side_index < 2 * (index);
side_index++)
cout << " ";
if (index != 0 && index != N - 1)
cout << Diagonal++;
else
cout << " ";
for (side_index = 0; side_index < 2 * (N - index - 1);
side_index++)
cout << " ";
cout << Right++;
cout << endl;
}
}
int main(int argc, char** argv){
int Size = 8;
print_N(Size);
return 0;
} ผลลัพธ์
1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8