Given an integer N, the task is to print the Alphabet N Pattern as given below:
1 1
2 2 2
3 3 3
* * *
* * *
* * *
N N
Examples:
Input: N = 6
Output:
1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6
Input: N = 5
Output:
1 1
2 2 2
3 3 3
4 4 4
5 5
Approach: Except the first and the last row, every other row will follow the following:
- Print the first value as index + 1 where index is the index of the row.
- Then print blank spaces 2 * index times.
- Again print the value index + 1 as the diagonal element for the current row.
- Then print the rest of the 2 * (N – index – 1) blank spaces followed by the ending element which is again index + 1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void Alphabet_N_Pattern( int N)
{
int index, side_index, size;
int Right = 1, Left = 1, Diagonal = 2;
for (index = 0; index < N; index++) {
cout << Left++;
for (side_index = 0; side_index < 2 * (index); side_index++)
cout << " " ;
if (index != 0 && index != N - 1)
cout << Diagonal++;
else
cout << " " ;
for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)
cout << " " ;
cout << Right++;
cout << endl;
}
}
int main( int argc, char ** argv)
{
int Size = 6;
Alphabet_N_Pattern(Size);
}
|
C
#include <stdio.h>
void Alphabet_N_Pattern( int N)
{
int index, side_index, size;
int Right = 1, Left = 1, Diagonal = 2;
for (index = 0; index < N; index++) {
printf ( "%d" , Left++);
for (side_index = 0; side_index < 2 * (index); side_index++)
printf ( " " );
if (index != 0 && index != N - 1)
printf ( "%d" , Diagonal++);
else
printf ( " " );
for (side_index = 0; side_index < 2 * (N - index - 1); side_index++)
printf ( " " );
printf ( "%d" , Right++);
printf ( "\n" );
}
}
int main( int argc, char ** argv)
{
int Size = 6;
Alphabet_N_Pattern(Size);
}
|
Java
import java.util.*;
class solution
{
static void Alphabet_N_Pattern( int N)
{
int index, side_index, size;
int Right = 1 , Left = 1 , Diagonal = 2 ;
for (index = 0 ; index < N; index++) {
System.out.print(Left++);
for (side_index = 0 ; side_index < 2 * (index); side_index++)
System.out.print( " " );
if (index != 0 && index != N - 1 )
System.out.print(Diagonal++);
else
System.out.print( " " );
for (side_index = 0 ; side_index < 2 * (N - index - 1 ); side_index++)
System.out.print( " " );
System.out.print(Right++);
System.out.println();
}
}
public static void main(String args[])
{
int Size = 6 ;
Alphabet_N_Pattern(Size);
}
}
|
Python3
def Alphabet_N_Pattern(N):
Right = 1
Left = 1
Diagonal = 2
for index in range (N):
print (Left, end = "")
Left + = 1
for side_index in range ( 0 , 2 * (index), 1 ):
print ( " " , end = "")
if (index ! = 0 and index ! = N - 1 ):
print (Diagonal, end = "")
Diagonal + = 1
else :
print ( " " , end = "")
for side_index in range ( 0 , 2 * (N - index - 1 ), 1 ):
print ( " " , end = "")
print (Right, end = "")
Right + = 1
print ( "\n" , end = "")
if __name__ = = '__main__' :
Size = 6
Alphabet_N_Pattern(Size)
|
C#
using System;
class GFG
{
public static void Alphabet_N_Pattern( int N)
{
int index, side_index;
int Right = 1, Left = 1, Diagonal = 2;
for (index = 0; index < N; index++)
{
Console.Write(Left++);
for (side_index = 0;
side_index < 2 * (index); side_index++)
Console.Write( " " );
if (index != 0 && index != N - 1)
Console.Write(Diagonal++);
else
Console.Write( " " );
for (side_index = 0;
side_index < 2 * (N - index - 1);
side_index++)
Console.Write( " " );
Console.Write(Right++);
Console.Write( "\n" );
}
}
static void Main()
{
int Size = 6;
Alphabet_N_Pattern(Size);
}
}
|
PHP
<?php
function Alphabet_N_Pattern( $N )
{
$index ;
$side_index ;
$size ;
$Right = 1;
$Left = 1;
$Diagonal = 2;
for ( $index = 0; $index < $N ; $index ++)
{
echo $Left ++;
for ( $side_index = 0;
$side_index < 2 * ( $index );
$side_index ++)
echo " " ;
if ( $index != 0 && $index != $N - 1)
echo $Diagonal ++;
else
echo " " ;
for ( $side_index = 0;
$side_index < 2 * ( $N - $index - 1);
$side_index ++)
echo " " ;
echo $Right ++;
echo "\n" ;
}
}
$Size = 6;
Alphabet_N_Pattern( $Size );
?>
|
Javascript
<script>
function Alphabet_N_Pattern(N) {
var index, side_index, size;
var Right = 1,
Left = 1,
Diagonal = 2;
for (index = 0; index < N; index++) {
document.write(Left++);
for (side_index = 0;
side_index < 2 * index; side_index++)
document.write( " " );
if (index != 0 && index != N - 1)
document.write(Diagonal++);
else
document.write( " " );
for (side_index = 0;
side_index < 2 * (N - index - 1); side_index++)
document.write( " " );
document.write(Right++);
document.write( "<br>" );
}
}
var Size = 6;
Alphabet_N_Pattern(Size);
</script>
|
Output: 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6
Time Complexity: O(n2)
Auxiliary Space: O(1)