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++
#include<bits/stdc++.h>
using namespace std;
void display( int n)
{
int v = n;
while (v >= 0)
{
int c = 65;
for ( int j = 0; j < v + 1; j++)
{
cout << char (c + j) << " " ;
}
v--;
cout << endl;
}
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;
}
}
int main()
{
int n = 5;
display(n);
return 0;
}
|
Java
public class Main
{
public static void display( int n)
{
int v = n;
while (v >= 0 )
{
int c = 65 ;
for ( int j = 0 ; j < v + 1 ; j++)
{
System.out.print(( char )(c + j) + " " );
}
v--;
System.out.println();
}
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();
}
}
public static void main(String[] args)
{
int n = 5 ;
display(n);
}
}
|
Python3
def display(n):
v = n
while ( v > = 0 ) :
c = 65
for j in range (v + 1 ):
print ( chr ( c + j ), end = " " )
v = v - 1
print ()
for i in range (n + 1 ):
c = 65
for j in range ( i + 1 ):
print ( chr ( c + j), end = " " )
print ()
n = 5
display(n)
|
C#
using System.IO;
using System;
class Gfg
{
static void display( int n)
{
int v = n;
while (v >= 0)
{
int c = 65;
for ( int j = 0; j < v + 1; j++)
{
Console.Write(( char )(c + j) + " " );
}
v--;
Console.WriteLine();
}
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();
}
}
static void Main()
{
int n = 5;
display(n);
}
}
|
Javascript
<script>
function display(n)
{
let v = n;
while (v >= 0)
{
let c = 65;
for (let j = 0; j < v + 1; j++)
{
document.write(String.fromCharCode(c + j) + " " );
}
v--;
document.write( "</br>" );
}
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);
</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)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!