Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรมพิมพ์รูปแบบตัวเลข (Numeric Pattern) ในภาษา C

คำอธิบายโปรแกรม

พิมพ์รูปแบบตัวเลข โดยรับจำนวนแถวจากผู้ใช้

อินพุต: 5 แถว

1
6 2
10 7 3
13 11 8 4
15 14 12 9 5

อัลกอริทึม

พิมพ์รูปแบบจากท้ายของแต่ละแถว
ทำให้คอลัมน์สุดท้ายของแต่ละแถวสมบูรณ์
เริ่มจากคอลัมน์ที่สองจากท้ายของแถวที่สอง
ทำซ้ำจนครบจำนวนแถวที่กำหนด

ตัวอย่างโค้ด

/* Program to print Numeric Pattern */
#include <stdio.h>

int main() {
    int k, l, m, count = 1;
    int rows;
    // clrscr();
    printf("\n Please enter the number of rows for the Numeric Pattern: ");
    scanf("%d", &rows);
    for (k = 1; k <= rows; k++) {
        m = count;
        for (l = 1; l <= k; l++) {
            printf("%d", m);
            m = m - (rows + l - k);
        }
        printf("\n");
        count = count + 1 + rows - k;
    }
    // getch();
    return 0;
}

เอาต์พุต

แสดงรูปแบบตัวเลขตามจำนวนแถวที่ป้อน