Use nested loops to produce a pyramid pattern based on letter - C Statement

C examples for Statement:for

Description

Use nested loops to produce a pyramid pattern based on letter

A pattern as follows:

      A 
     ABA
    ABCBA
   ABCDCBA
  ABCDEDCBA

Demo Code


#include <stdio.h>

void print_spaces(unsigned int n);

int main(void)
{
  char uppercase_letter;
  char c1, c2;//from w w  w .j  a v a 2 s  .  c  o m

  do // get uppercase letter from user
  {
    printf("Enter an uppercase letter: ");
    scanf(" %c", &uppercase_letter);
  } while (uppercase_letter < 'A' || 'Z' < uppercase_letter);

  for(c1 = 'A'; c1 <= uppercase_letter; c1++)
  {
    // print opening spaces
    print_spaces(uppercase_letter - c1);

    // print letters
    // ascending
    for (c2 = 'A'; c2 < c1; c2++)
    {
      printf("%c", c2);

    }
    // descending
    for (; 'A' <= c2; c2--)
    {
      printf("%c", c2);
    }

    // print closing spaces
    print_spaces(uppercase_letter - c1);
    printf("\n");
  }

  return 0;
}

void print_spaces(unsigned int n)
{
  for (int i = 0; i < n; i++)
  {
    printf(" ");
  }
}

Result


Related Tutorials