C Program to Print all English Alphabets Repeated n Times

Write a c program to print all the English alphabet repeated n times.

The program to print the English alphabets n times is very simple. First, we will see how to print all the English alphabets. The following code prints all English alphabets.

char c;
for (c = 'A'; c <= 'Z'; ++c) {
	printf("%c ", c);
}

Some of you may be confused about how the code works. In C programming, each character corresponds to a numeric value, known as the ASCII value. A variable of type ‘char’ stores the numeric ASCII value. The ASCII value from ‘A’ to ‘Z’ is contiguous ( A=64, B=65, C=66, and so on ). Thus, we start from ‘A’ and keep incrementing ASCII value of the character to move to the next character.

The program asks the user to input n. We will run the above code n times using an outer loop to print alphabets n times.

Program to Print all English Alphabets Repeated n Times

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int n, i;
	char c;

	printf("Enter n: ");
	scanf("%d", &n);

	// the outer loop runs n times
	for (i = 0; i < n; ++i) {
		// the inner loop prints english alphabets from 'A' to 'Z'
		for (c = 'A'; c <= 'Z'; ++c) {
			printf("%c ", c);
		}
		printf("\n");
	}

}

Output

Program to Print all English Alphabets Repeated n Times using Recursion

Each recursive call will print all alphabets from ‘A’ to ‘Z’ once. We will decrement n on each recursive call. The recursive call ends when the value of n becomes zero. In this way, we print the alphabets n times.

#include<stdio.h>
#include<stdlib.h>

void PrintAlphabets(int n) {

	if (n <= 0)
		return;

	char c;
	for (c = 'A'; c <= 'Z'; ++c) {
		printf("%c ", c);
	}
	printf("\n");

	PrintAlphabets(n - 1);

}

int main()
{
	int n, i;
	char c;

	printf("Enter n: ");
	scanf("%d", &n);

	PrintAlphabets(n);

}

Output

Read

Leave a Comment

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