jeudi 3 novembre 2016

Alphabetic Pattern Capital Letters Pyramid C Programing

I got pretty stuck when I had a to do a letter pyramid in C (via Xcode). We have to input the number of rows which will be equivalent to the number of capital letters. This is what it should look like :

Enter number of rows : 4

Output:

   A
  ABA
 ABCBA
ABCDCBA

It is limited to min: 4 and Max 26. This is what I've managed so far. Thank you in advance :

int main(){

    char letter = 'A';  //NOTE ! letter + 1 = 'B'
    int i, rows, space, k = 0;

    printf("Enter number of rows: \t");
        scanf("%i", &rows);



    while(rows < 4 || rows > 26){
        printf("\nError !\nMin : 4\tMax : 26\n");
            scanf("%i", &rows);

    }



    for(i = 1; i <= rows; i++,k =0){
        for(space = 1; space <= rows - i; space++){
            printf(" ");

        }

        for(; k!= 2* i-1; k++){
            printf("%c", letter);
        }

        for (char temp = letter + 1; temp <= letter; temp--){
            printf("%c", temp);
        }

        printf("\n");
    }



    return 0;
}

Output:

Enter number of rows: 4

   A
  AAA
 AAAAA
AAAAAAA

Program ended with exit code: 0

Aucun commentaire:

Enregistrer un commentaire