This C program generates a pyramid-like tower pattern using digits. The code utilizes functions for printing rows and constructing the entire tower. The pattern is printed by calling printDigitsTower() in the main() function with a height of 6.

#include <stdio.h>

void printRow(int numDigits, int startDigit) {
    for (int i = 0; i < numDigits; i++) {
        printf('%d', (startDigit + i) % 10);
    }
    printf('\n');
}

void printDigitsTower(int height) {
    int numDigits = height * 2 - 1;
    int startDigit = 1;

    for (int i = 0; i < height; i++) {
        printRow(numDigits, startDigit);
        numDigits -= 2;
        startDigit = (startDigit + 1) % 10;
    }
}

int main() {
    int height = 6;
    printDigitsTower(height);

    return 0;
}

Explanation:

  • printRow(int numDigits, int startDigit): This function prints a single row of the tower. It takes the number of digits in the row (numDigits) and the starting digit (startDigit) as arguments. It then iterates through the specified number of digits, printing each digit and wrapping around to 0 if the digit exceeds 9.
  • printDigitsTower(int height): This function generates the entire tower pattern based on the provided height. It calculates the number of digits in the first row and the starting digit, and then iteratively calls printRow() to print each row, decreasing the number of digits and incrementing the starting digit for subsequent rows.
  • main(): The main() function sets the height of the tower to 6 and calls printDigitsTower() to generate and print the tower pattern.

When you run this program, you will see the following output:

123456789012345
234567890123
3456789012
45678901
567890
6789

Please note that the alignment of the pattern may vary depending on the console or terminal you are using.

C Program to Print Digits Tower Pattern

原文地址: https://www.cveoy.top/t/topic/REB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录