C Programming

Solved Example

Practice Questions

Exercise Question

program to print the following pattern of alphabets

Objective

Write a c program to receive input for the number of rows and print the following pattern of alphabets

ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE   EDCBA
ABCD     DCBA
ABC       CBA
AB         BA
A           A

Program

#include "stdio.h"
int main() {
	int i,j,k,l,n;

	printf("Enter the number of rows: ");
	scanf("%d",&n);
	
	for(i = n; i >= 1; i--) {
		for(j = 1; j <= i; j++) {
			printf("%c",j+64);
		}
		for(k = 1; k <= 2 * (n - i) - 1;k++) {
			printf(" ");
		}
		for(l = i; l >= 1; l--) {
			if(l == n) {
				continue;
			}
			printf("%c",l+64);
		}
		
		printf("\n");
	}
	return 0;
}

Solution

Enter the number of rows: 7
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE   EDCBA
ABCD     DCBA
ABC       CBA
AB         BA
A           A