Alphabet pattern programs in C/C++

Alphabet pattern programs in C/C++

alphabet pattern programs output

Pattern programs are widely asked questions in university exams and interviews. These type of questions test logic and concept of a programmer. Every pattern requires different logic. All type of patterns use nested for loops. Some of the pattern uses more than two for loops. Let’s see some alphabet pattern programs in C/C++.

Also read:
1. Star pattern programs in C – 1
2. Star pattern programs in C/C++ – 2
3. Number pattern programs in C/C++.

1. Alphabet triangle – 1

Output-

pattern programs in C/C++

Program in C :

/*
 A
 B C
 D E F
 G H I J
*/
#include<stdio.h>
int main() {
  int i, j, n, x;
  printf("\n Enter no of rows: ");
  scanf("%d", & n);
  for (x = 65, i = 1; i <= n; i++) {
    for (j = n - i; j < n; j++, x++)
      printf(" %c", x);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 A
 B C
 D E F
 G H I J
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, n, x;
  cout << "\n Enter no of rows: ";
  cin >> n;
  for (x = 65, i = 1; i <= n; i++) {
    for (j = n - i; j < n; j++, x++)
      cout << " " << char(x);
    cout << "\n";
  }
  return 0;
}

Note: “%c” in C and char() in C++ are used to print the Alphabet having ASCII of that integer value. E.g : (“%c”,65 ) = char(65) = A

2. Alphabet triangle – 2

Output-

pattern programs in C/C++

Program in C :

/*
 A
 A B
 A B C
 A B C D
*/
#include<stdio.h>
int main() {
  int i, j, n, x;
  printf("\n Enter no of rows: ");
  scanf("%d", & n);
  for (i = 1; i <= n; i++) {
    for (x = 65, j = n - i; j < n; j++, x++)
      printf(" %c", x);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 A
 A B
 A B C
 A B C D
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, n, x;
  cout << "\n Enter no of rows: ";
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (x = 65, j = n - i; j < n; j++, x++)
      cout << " " << char(x);
    cout << "\n";
  }
  return 0;
}

3. Alphabet triangle – 3

Output-

pattern programs in C/C++

Program in C :

/*
 A
 B B
 C C C
 D D D D
*/
#include<stdio.h>
int main() {
  int i, j, n, x = 65;
  printf("\n Enter no of rows: ");
  scanf("%d", & n);
  for (i = 1; i <= n; i++, x++) {
    for (j = n - i; j < n; j++)
      printf(" %c", x);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 A
 B B
 C C C
 D D D D
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, n, x = 65;
  cout << "\n Enter no of rows: ";
  cin >> n;
  for (i = 1; i <= n; i++, x++) {
    for (j = n - i; j < n; j++)
      cout << " " << char(x);
    cout << "\n";
  }
  return 0;
}

4. Stars around Alphabets square pattern

Output-

pattern programs in C/C++

Program in C :

/*
 * * * * *
 * A B C *
 * D E F *
 * G H I *
 * * * * *
*/
#include<stdio.h>
int main() {
  int i, j, r;
  char ch = 'A';
  printf("\n Enter number of rows: ");
  scanf("%d", & r);
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= r; j++) {
      if (j == 1 || i == 1 || j == r || i == r)
        printf(" *");
      else
        printf(" %c", ch++);
      if (ch > 'Z')
        ch = 'A';
    }
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 * * * * *
 * A B C *
 * D E F *
 * G H I *
 * * * * *
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, r;
  char ch = 'A';
  cout << "\n Enter number of rows: ";
  cin >> r;
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= r; j++) {
      if (j == 1 || i == 1 || j == r || i == r)
        cout << " *";
      else
        cout << " " << ch++;
      if (ch > 'Z')
        ch = 'A';
    }
    cout << "\n";
  }
  return 0;
}

Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. print/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.
9. char(x) is a function used to print the character with ‘x’ ASCII value.

Conclusion-

So that’s how we print different alphabet pattern programs in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

2 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *