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

โปรแกรมพิมพ์รูปธงขวาและธงซ้ายในภาษา C

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

พิมพ์รูปแบบธงขวา (Right Arrow) และธงซ้าย (Left Arrow)

อัลกอริทึม

รับจำนวนแถวสำหรับพิมพ์รูปแบบธงซ้ายและขวา

พิมพ์ส่วนบนของธงด้วยรูปแบบดาว
พิมพ์สามเหลี่ยมมุมฉากผกผันด้วยดาว
พิมพ์ส่วนล่างของธงด้วยรูปแบบดาว
พิมพ์สามเหลี่ยมมุมฉากด้วยดาว

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

/* Program to print the Left and right arrow pattern */
#include <stdio.h>

int main() {
    int r, c, rows; // Left Arrow Pattern
    int r1, c1, rows1; // Right Arrow Pattern
    // clrscr();
    printf("Enter number of rows to print the Left Arrow Pattern: ");
    scanf("%d", &rows);
    printf("\n");
    printf("The Left Arrow Pattern is:\n");
    // ส่วนบนของธงซ้าย
    for (r = 1; r < rows; r++) {
        for (c = 1; c <= (rows - r); c++) {
            printf(" ");
        }
        for (c = r; c <= rows; c++) {
            printf("*");
        }
        printf("\n");
    }
    // ส่วนล่างของธงซ้าย
    for (r = 1; r <= rows; r++) {
        for (c = 1; c < r; c++) {
            printf(" ");
        }
        for (c = 1; c <= r; c++) {
            printf("*");
        }
        printf("\n");
    }
    printf("Enter number of rows to print the Right Arrow Pattern: ");
    scanf("%d", &rows1);
    printf("\n");
    printf("The Right Arrow Pattern is:\n");
    // ส่วนบนของธงขวา
    for (r1 = 1; r1 < rows1; r1++) {
        for (c1 = 1; c1 <= (2 * r1 - 2); c1++) {
            printf(" ");
        }
        for (c1 = r1; c1 <= rows1; c1++) {
            printf("*");
        }
        printf("\n");
    }
    // ส่วนล่างของธงขวา
    for (r1 = 1; r1 <= rows1; r1++) {
        for (c1 = 1; c1 <= (2 * rows1 - 2 * r1); c1++) {
            printf(" ");
        }
        for (c1 = 1; c1 <= r1; c1++) {
            printf("*");
        }
        printf("\n");
    }
    // getch();
    return 0;
}

เอาต์พุต

แสดงรูปธงซ้ายและธงขวา ตามจำนวนแถวที่ป้อนเข้าไป