GeeksforGeeks App
Open App
Browser
Continue

# Program to print V and inverted-V pattern

Inverted V pattern: Given the value of n, print the inverted V pattern.
Examples :

```Input : n = 5
Output :

E
D D
C   C
B     B
A       A

Input : n = 7
Output :

G
F F
E   E
D     D
C       C
B         B
A           A```

Below is the program to print the above pattern

## C++

 `// C++ Implementation to print the pattern``#include ``using` `namespace` `std;`` ` `// Function definition``void` `pattern(``int` `n)``{``    ``int` `i, j, k = 0;``    ``for` `(i = n - 1; i >= 0; i--) {`` ` `        ``// outer gap loop``        ``for` `(j = n - 1; j > k; j--) {``            ``cout << ``" "``;``        ``}`` ` `        ``// 65 is ASCII of 'A'``        ``cout << ``char``(i + 65);`` ` `        ``// inner gap loop``        ``for` `(j = 1; j < (k * 2); j++)``            ``cout << ``" "``;`` ` `        ``if` `(i < n - 1)``            ``cout << ``char``(i + 65);``        ``cout << ``"\n"``;``        ``k++;``    ``}``}`` ` `// Driver code``int` `main()``{``    ``// taking size from the user``    ``int` `n = 5;`` ` `    ``// function calling``    ``pattern(n);``    ``return` `0;``}`

## Java

 `// Java Implementation to print the pattern``import` `java.io.*;`` ` `class` `GFG``{``     ``// Function definition``   ``static` `void` `pattern(``int` `n)``    ``{``        ``int` `i, j, k = ``0``;``        ``for` `(i = n - ``1``; i >= ``0``; i--) ``        ``{``            ``// outer gap loop``            ``for` `(j = n - ``1``; j > k; j--) ``            ``{``                ``System.out.print(``" "``);``            ``}`` ` `            ``// 65 is ASCII of 'A'``            ``System.out.print((``char``)(i + ``65``));`` ` `            ``// inner gap loop``            ``for` `(j = ``1``; j < (k * ``2``); j++)``                ``System.out.print(``" "``);`` ` `            ``if` `(i < n - ``1``)``                ``System.out.print((``char``)(i + ``65``));``            ``System.out.print(``"\n"``);``            ``k++;``        ``}``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``         ` `        ``// taking size from the user``        ``int` `n = ``5``;``     ` `        ``// function calling``        ``pattern(n);``    ``}``}`` ` `// This code is contributed by Anshika Goyal.`

## Python3

 `# Python Implementation to print ``# the pattern `` ` `# Function definition ``def` `pattern(n):``    ``k ``=` `0``    ``for` `i ``in` `range``(n ``-` `1``, ``-``1``, ``-``1``):`` ` `        ``# outer gap loop ``        ``for` `j ``in` `range``(n ``-` `1``, k, ``-``1``):``            ``print``(``' '``, end ``=` `'')`` ` `        ``# 65 is ASCII of 'A' ``        ``print``(``chr``(i ``+` `65``), end ``=` `'')`` ` `        ``# inner gap loop ``        ``for` `j ``in` `range``(``1``, k ``*` `2``):``            ``print``(``' '``, end ``=` `'')``        ``if` `i

## C#

 `// C# Implementation to print the pattern``using` `System;`` ` `class` `GFG``{``    ``// Function definition``static` `void` `pattern(``int` `n)``    ``{``        ``int` `i, j, k = 0;``        ``for` `(i = n - 1; i >= 0; i--)``        ``{``            ``// outer gap loop``            ``for` `(j = n - 1; j > k; j--)``            ``{``                ``Console.Write(``" "``);``            ``}`` ` `            ``// 65 is ASCII of 'A'``            ``Console.Write((``char``)(i + 65));`` ` `            ``// inner gap loop``            ``for` `(j = 1; j < (k * 2); j++)``                ``Console.Write(``" "``);`` ` `            ``if` `(i < n - 1)``                ``Console.Write((``char``)(i + 65));``                ``Console.WriteLine();``            ``k++;``        ``}``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``         ` `        ``// taking size from the user``        ``int` `n = 5;``     ` `        ``// function calling``        ``pattern(n);``    ``}``}`` ` `// This code is contributed by vt_m.`

## PHP

 `= 0; ``\$i``--) ``    ``{`` ` `        ``// outer gap loop``        ``for` `(``\$j` `= ``\$n` `- 1; ``\$j` `> ``\$k``; ``\$j``--) ``        ``{``            ``echo` `" "``;``        ``}`` ` `        ``// 65 is ASCII of 'A'``        ``echo` `chr``(``\$i` `+ 65);`` ` `        ``// inner gap loop``        ``for` `(``\$j` `= 1; ``\$j` `< (``\$k` `* 2); ``\$j``++)``            ``echo` `" "``;`` ` `        ``if` `(``\$i` `< ``\$n` `- 1)``            ``echo` `chr``(``\$i` `+ 65);`` ` `        ``echo` `"\n"``;``        ``\$k``++;``    ``}``}`` ` `// Driver code``\$n` `= 5;``pattern(``\$n``);`` ` `// This code is contributed by mits ``?>`

## Javascript

 ``

Output:

```    E
D D
C   C
B     B
A       A```

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

V pattern: Given the value of n, print the V pattern.
Examples :

```Input : n = 5
Output :

E       E
D     D
C   C
B B
A

Input : n = 7
Output :

G           G
F         F
E       E
D     D
C   C
B B
A```

Below is the program to print the above pattern

## C++

 `// C++ Implementation to print the pattern``#include ``using` `namespace` `std;`` ` `// Function definition``void` `pattern(``int` `n)``{``    ``int` `i, j;``    ``for` `(i = n - 1; i >= 0; i--) {`` ` `        ``// outer gap loop``        ``for` `(j = n - 1; j > i; j--) {``            ``cout << ``" "``;``        ``}`` ` `        ``// 65 is ASCII of 'A'``        ``cout << ``char``(i + 65);`` ` `        ``// inner gap loop``        ``for` `(j = 1; j < (i * 2); j++)``            ``cout << ``" "``;`` ` `        ``if` `(i >= 1)``            ``cout << ``char``(i + 65);``        ``cout << ``"\n"``;``    ``}``}`` ` `// Driver code``int` `main()``{``    ``// taking size from the user``    ``int` `n = 5;`` ` `    ``// function calling``    ``pattern(n);``    ``return` `0;``}`

## Java

 `// Java Implementation to print the pattern``import` `java.io.*;`` ` `class` `GFG``{``    ``// Function definition``    ``static` `void` `pattern(``int` `n)``    ``{``        ``int` `i, j;``        ``for` `(i = n - ``1``; i >= ``0``; i--) ``        ``{``            ``// outer gap loop``            ``for` `(j = n - ``1``; j > i; j--) ``            ``{``                ``System.out.print(``" "``);``            ``}`` ` `            ``// 65 is ASCII of 'A'``            ``System.out.print((``char``)(i + ``65``));`` ` `            ``// inner gap loop``            ``for` `(j = ``1``; j < (i * ``2``); j++)``                ``System.out.print(``" "``);`` ` `            ``if` `(i >= ``1``)``                ``System.out.print((``char``)(i + ``65``));``            ``System.out.print(``"\n"``);``        ``}``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `main(String args[])``    ``{``        ``// taking size from the user``        ``int` `n = ``5``;``     ` `        ``// function calling``        ``pattern(n);``    ``}``}`` ` `// This code is contributed by Anshika Goyal.`

## Python3

 `# Python3 Implementation to print``# the pattern``import` `math as mt`` ` `# Function definition``def` `pattern(n):`` ` `    ``i ``=` `n ``-` `1``    ``j ``=` `1``    ``for` `i ``in` `range``(n ``-` `1``, ``-``1``, ``-``1``):`` ` `        ``# outer gap loop``        ``for` `j ``in` `range``(n ``-` `1``, i, ``-``1``):``            ``print``(``' '``, end ``=` `'')``             ` `        ``# 65 is ASCII of 'A'``        ``print``(``chr``(i ``+` `65``), end ``=` `'')`` ` `        ``# inner gap loop``        ``for` `j ``in` `range``(``1``, i ``*` `2``):``            ``print``(``' '``, end ``=` `'')`` ` `        ``if` `(i >``=` `1``):``            ``print``(``chr``(i ``+` `65``), end ``=` `'')``        ``print``()``         ` `# Driver code`` ` `# taking size from the user``n ``=` `5`` ` `# function calling``pattern(n)`` ` `# This code is contributed ``# by MOhit kumar`

## C#

 `// C# Implementation to print the pattern``using` `System;`` ` `class` `GFG``{``    ``// Function definition``    ``static` `void` `pattern(``int` `n)``    ``{``        ``int` `i, j;``        ``for` `(i = n - 1; i >= 0; i--) ``        ``{``            ``// outer gap loop``            ``for` `(j = n - 1; j > i; j--) ``            ``{``             ``Console.Write(``" "``);``            ``}`` ` `            ``// 65 is ASCII of 'A'``            ``Console.Write((``char``)(i + 65));`` ` `            ``// inner gap loop``            ``for` `(j = 1; j < (i * 2); j++)``                ``Console.Write(``" "``);`` ` `            ``if` `(i >= 1)``                ``Console.Write((``char``)(i + 65));``            ``Console.WriteLine(``""``);``        ``}``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``// taking size from the user``        ``int` `n = 5;``     ` `        ``// function calling``        ``pattern(n);``    ``}``}`` ` `// This code is contributed by vt_m.`

## PHP

 `= 0; ``\$i``--) ``    ``{`` ` `        ``// outer gap loop``        ``for` `(``\$j` `= ``\$n` `- 1; ``\$j` `> ``\$i``; ``\$j``--) ``        ``{``            ``echo` `" "``;``        ``}`` ` `        ``// 65 is ASCII of 'A'``        ``echo` `chr``(``\$i` `+ 65);`` ` `        ``// inner gap loop``        ``for` `(``\$j` `= 1; ``\$j` `< (``\$i` `* 2); ``\$j``++)``            ``echo` `" "``;`` ` `        ``if` `(``\$i` `>= 1)``            ``echo` `chr``(``\$i` `+ 65);``             ` `        ``echo` `"\n"``;``    ``}``}`` ` `// Driver code``\$n` `= 5;``pattern(``\$n``);`` ` `// This code is contributed by mits ``?>`

## Javascript

 ``

Output:

```E       E
D     D
C   C
B B
A```

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

My Personal Notes arrow_drop_up