Open In App

Program to Print K using Alphabets

Last Updated : 09 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n, the task is to print ‘K’ using alphabets.
Examples: 

Input: n = 5
Output: 
A B C D E F 
A B C D E 
A B C D 
A B C 
A B 
A 
A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F 

Input: n = 3
Output:
A B C D 
A B C 
A B 
A 
A 
A B 
A B C 
A B C D

Below is the implementation.
 

C++




// C++ Program to design the
// above pattern of K using alphabets
#include<bits/stdc++.h>
using namespace std;
 
// Function to print
// the above Pattern
void display(int n)
{
  int v = n;
 
  // This loop is used
  // for rows and prints
  // the alphabets in
  // decreasing order
  while (v >= 0)
  {
    int c = 65;
 
    // This loop is used
    // for columns
    for(int j = 0; j < v + 1; j++)
    {
      // chr() function converts the
      // number to alphabet
      cout << char(c + j) << " ";
    }
 
    v--;
    cout << endl;
  }
 
  // This loop is again used
  // to rows and prints the
  // half remaining pattern in
  // increasing order
  for(int i = 0; i < n + 1; i++)
  {
    int c = 65;
 
    for(int j = 0; j < i + 1; j++)
    {
      cout << char(c + j) << " ";
    }
    cout << endl;
  }
}
 
// Driver code
int main()
{
  int n = 5;
  display(n);
  return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java




// Java Program to design the
// above pattern of K using alphabets
public class Main
{
    // Function to print
    // the above Pattern
    public static void display(int n)
    {
      int v = n;
      
      // This loop is used
      // for rows and prints
      // the alphabets in
      // decreasing order
      while (v >= 0)
      {
        int c = 65;
      
        // This loop is used
        // for columns
        for(int j = 0; j < v + 1; j++)
        {
           
          // chr() function converts the
          // number to alphabet
          System.out.print((char)(c + j) + " ");
        }
      
        v--;
        System.out.println();
      }
      
      // This loop is again used
      // to rows and prints the
      // half remaining pattern in
      // increasing order
      for(int i = 0; i < n + 1; i++)
      {
        int c = 65;
      
        for(int j = 0; j < i + 1; j++)
        {
          System.out.print((char)(c + j) + " ");
        }
        System.out.println();
      }
    }
   
  // Driver code
  public static void main(String[] args)
  {
    int n = 5;
    display(n);
  }
}
 
// This code is contributed by divyesh072019


Python3




# Python Program to design the
# above pattern of K using alphabets
  
# Function to print the above Pattern
def display(n):
    v = n
     
    # This loop is used for rows and
    # prints the alphabets in decreasing
    # order
    while ( v >= 0) :
        c = 65
         
        # This loop is used for columns
        for j in range(v + 1):
             
            # chr() function converts the
            # number to alphabet
            print( chr ( c + j ), end = " ")
        v = v - 1
        print()
         
    # This loop is again used to rows and
    # prints the half remaining pattern in
    # increasing order
    for i in range(n + 1):
        c = 65
         
        for j in range( i + 1):
            print( chr ( c + j), end =" ")
        print()
 
# Driver code
n = 5
display(n)


C#




// C# Program to design the
// above pattern of K using alphabets
using System.IO;
using System;
 
class Gfg
{
   
    // Function to print
    // the above Pattern
    static void display(int n)
    {
        int v = n;
       
        // This loop is used
        // for rows and prints
        // the alphabets in
        // decreasing order
        while(v >= 0)
        {
            int c = 65;
           
            // This loop is used
            // for columns
            for(int j = 0; j < v + 1; j++)
            {
               
                // chr() function converts the
                // number to alphabet
                Console.Write((char)(c + j) + " ");
            }
            v--;
            Console.WriteLine();
        }
       
        // This loop is again used
        // to rows and prints the
        // half remaining pattern in
        // increasing order
        for(int i = 0; i < n + 1; i++)
        {
            int c = 65;
            for(int j = 0; j < i + 1; j++)
            {
                Console.Write((char)(c + j) + " ");
            }
            Console.WriteLine();
        }
    }
   
    // Driver code
    static void Main()
    {
        int n = 5;
        display(n);
    }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript




<script>
    // Javascript Program to design the
    // above pattern of K using alphabets
     
    // Function to print
    // the above Pattern
    function display(n)
    {
      let v = n;
 
      // This loop is used
      // for rows and prints
      // the alphabets in
      // decreasing order
      while (v >= 0)
      {
        let c = 65;
 
        // This loop is used
        // for columns
        for(let j = 0; j < v + 1; j++)
        {
          // chr() function converts the
          // number to alphabet
          document.write(String.fromCharCode(c + j) + " ");
        }
 
        v--;
        document.write("</br>");
      }
 
      // This loop is again used
      // to rows and prints the
      // half remaining pattern in
      // increasing order
      for(let i = 0; i < n + 1; i++)
      {
        let c = 65;
 
        for(let j = 0; j < i + 1; j++)
        {
          document.write(String.fromCharCode(c + j) + " ");
        }
        document.write("</br>");
      }
    }
     
    let n = 5;
      display(n);
     
    // This code is contributed by suresh07.
</script>


Output: 

A B C D E F 
A B C D E 
A B C D 
A B C 
A B 
A 
A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F

Time Complexity: O(N)

Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Count of ways to rearrange N digits and M alphabets keeping all alphabets together
Given two positive integers N and M representing the count of distinct digits and alphabets respectively in a string, the task to count the number of ways to rearrange the characters of the string such that all the alphabets are adjacent. Examples: Input: N = 2, M = 2Output: 12Explanation: Possible ways to rearrange characters of a string such that
9 min read
Program to Print Alphabets From A to Z Using Loop
Our task is to print the alphabets from A to Z using loops. There are various methods to print alphabets from (A to Z) or (a to z). Using ASCII valuesUsing character variables.In this article we will mainly focus on the following programs and their logic: Using for loopUsing the while loopUsing a do-while loop Recommended: Please try your approach
15 min read
Program to print the Alphabets of a Given Word using * pattern
Given a word str, the task is to print the alphabets of this word using * pattern.Examples: Input: GFGOutput: ****** * * * * **** * * * * * * *** * ******* ** ** ****** ** ** ** ****** * * * * **** * * * * * * *** *Approach: A simple implementation is to form the pattern for each letter A-Z and call them as functions.Below is the implementation of
15+ min read
Python | Print Alphabets till N
Sometimes, while working with Python, we can have a problem in which we need to print the specific number of alphabets in order. This can have application in school-level programming. Let's discuss certain ways in which this problem can be solved. Method #1 : Using loop + chr() This is brute force way to perform this task. In this, we iterate the e
6 min read
Print the Alphabets A to Z in Star Pattern
Given any alphabet between A to Z, the task is to print the pattern of the given alphabet using star. Examples: Input: A Output: ** * * ****** * * * * Input: P Output: ***** * * ***** * * Approach: The code to print each alphabet is created in a separate function. Use switch statement to call the desired function on the basis of the alphabet's patt
15+ min read
Program for triangular patterns of alphabets
Pattern printing has always been an interesting topic in programming languages. It emphasizes the usage of inner and outer loops in nested loops and creates beautiful patterns from them. Below are given some patterns using alphabets. Our task is to write programs for the below-given patterns, such that each pattern should print on the basis of give
14 min read
Python program to generate a list of alphabets in lexical order
Prerequisite: chr() The following methods explain how a python list with alphabets in lexical(alphabetic) order can be generated dynamically using chr() method. Approach: The core of the concept in both methods is almost same, they only differ in implementation: Validate size of alphabet list(size=26).If size&gt;26, change to 26.If size≤0, no meani
3 min read
Python Program to test if the String only Numbers and Alphabets
Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Explanation : Doesn't contain number. Method #1 : Usin
4 min read
Python program to separate alphabets and numbers in a string
Given a string containing numbers and alphabets, the task is to write a Python program to separate alphabets and numbers from a string using regular expression. Examples Input: abcd11gdf15hnnn678hh4 Output: 11 15 678 4 abcd gdf hnnn hh Explanation: The string is traversed from left to right and number and alphabets are separated from the given stri
4 min read
Python | Ways to initialize list with alphabets
While working with Python lists, sometimes we wish to initialize the list with the English alphabet a-z or A-Z. This is an essential utility in competitive programming and also in certain applications. Let's discuss various methods to achieve this using Python. Initialize the list with alphabets using string.ascii_uppercase The most pythonic and la
3 min read