Write a Program to Print the Following Pattern

Posted in

Write a Program to Print the Following Pattern
vinaykhatri

Vinay Khatri
Last updated on April 19, 2024

    Problem Statement

    We need to write a script in C, C++, and Python that can print the following pattern of Alphabets based on the user's entered letter.

    Input

    letter = F

    Output

    A
    AB
    ABC
    ABCD
    ABCDE
    ABCDEF

    Algorithm

    • Ask the user to enter the height of the pattern h which ranges between 1 to 26 (A to Z).
    • Create an outer loop i from range 1 to h .
    • Inside the outer loop create a nested inner loop j range from 1 to i .
    • Inside the inner loop, we will print the series of Alphabets using their ASCII code.
    • The ASCII code for the Upper Case letter starts from 65.

    C Program to Print a tringle of Alphabets.

    #include <stdio.h>
    int main()
    {
    	int h, i, j;
    	char letter;
    	//ask user to input the height of the pattern
    	printf("Enter the height of pattern(1-26): ");
    	scanf("%d", &h);
    	
    	for(i=1;i<=h;i++)
    	{
    		for(j=1;j<=i;j++)
    		{
    			//convert the integer ASCII code
    			//to equivalent character value
    			letter = j+64;
    		
    			printf("%c",letter);
    		}
    		
    		//print a new line after every row of alphabet series
    		printf("\n");
    	}
    
        return 0;
    }

    Output

    Enter the height of pattern(1-26): 5
    A
    AB
    ABC
    ABCD
    ABCDE

    C++ Program to Print a tringle of Alphabets

    #include <ioistream>
    using namespace std;
    
    int main()
    {
    	int h, i, j;
    	char letter;
    	//ask user to input the height of the pattern
    	cout<<"Enter the height of pattern(1-26): "; cin>>h;
    	
    	for(i=1;i<=h;i++)
    	{
    		for(j=1;j<=i;j++)
    		{
    			//convert the integer ASCII code
    			//to equivalent character value
    			letter = j+64;
    		
    			cout<<letter;
    		}
    		
    		//print a new line after every row of alphabet series
    		cout<<endl;
    	}
        return 0;
    }

    Output

    Enter the height of pattern(1-26): 6
    A
    AB
    ABC
    ABCD
    ABCDE
    ABCDEF

    Python Program to Print a tringle of Alphabets.

    #ask user to input the height of the pattern
    h = int(input("Enter the height of pattern(1-26): "))
    
    #outer loop
    for i in range(1,h+1):
    
        for j in range(1, i+1):
            # convert the integer ASCII code
            # to equivalent character value
            letter = chr(64+j)
            print(letter, end="")
    
        #print a new line after every row of alphabet series
        print()

    Output

    Enter the height of pattern(1-26): 8
    A
    AB
    ABC
    ABCD
    ABCDE
    ABCDEF
    ABCDEFG
    ABCDEFGH

    Complexity Analysis

    • Time Complexity: O(N 2 ), In the program, we are using a nested loop, that makes the time complexity Quadratic.
    • Space Complexity: O(1), there is no extra space usage in the program so the space complexity is constant.

    Wrapping Up!

    Now let's wrap this programming tutorial on "Write a Program to Print the pattern of Alphabets". You may find this problem in your upcoming programming interview, so it's better to learn its logic. All we have done in the program to convert a specific range of ASCII code into characters and print them repeatedly.

    People are also reading:

    Leave a Comment on this Post

    0 Comments