Skip to content
Related Articles
Program to print reverse character bridge pattern
• Difficulty Level : Medium
• Last Updated : 17 Apr, 2018

For a given value N, denoting the number of Charters starting from the A, print reverse character bridge pattern.

Examples :

```Input : n = 5
Output :

ABCDEDCBA
ABCD DCBA
ABC   CBA
AB     BA
A       A

Input : n = 8
Output :

ABCDEFGHGFEDCBA
ABCDEFG GFEDCBA
ABCDEF   FEDCBA
ABCDE     EDCBA
ABCD       DCBA
ABC         CBA
AB           BA
A             A
```

## Recommended: Please solve it on PRACTICE first, before moving on to the solution.

• For a given value N, reflect the number of characters taking part in the pattern, starting from A. For N = 5, Participating character would be A B C D E.
• By using a nested for loop we would compute the logic. Where the outer loop of ‘i’ would range from 0 to N and the inner loop of ‘j’ would range from 65(Start) to 64 + 2*N.
• Under which we would check the required condition for the pattern design. For all the values of j which are less than ((64+n)+ i) it would print the (char)((64 + n)-( j % (64+n))) and for all the values of j <= ((64+n) -i) it would print (char)j.

## C++

 `// CPP program to print reverse character bridge pattern``#include ``using` `namespace` `std;`` ` `// Function to print pattern``void` `ReverseCharBridge(``int` `n)``{``    ``for` `(``int` `i = 0; i < n; i++) ``    ``{``        ``for` `(``int` `j = ``'A'``; j < ``'A'` `+ (2 * n) - 1; j++) ``        ``{``            ``if` `(j >= (``'A'` `+ n - 1) + i)``                ``cout << (``char``)((``'A'` `+ n - 1) - ``                               ``(j % (``'A'` `+ n - 1)));``            ``else` `if` `(j <= (``'A'` `+ n - 1) - i)``                ``cout << (``char``)j;``            ``else``                ``cout << ``" "``;``        ``}``        ``cout << endl;``    ``}``}`` ` `// Driver Code``int` `main()``{``    ``int` `n = 6;``    ``ReverseCharBridge(n);``    ``return` `0;``}`

## Java

 `// Java program to print reverse``// character bridge pattern``import` `java.io.*;`` ` `class` `GFG {``     ` `    ``// Function to print pattern``    ``static` `void` `ReverseCharBridge(``int` `n)``    ``{``        ``for` `(``int` `i = ``0``; i < n; i++) ``        ``{``            ``for` `(``int` `j = ``'A'``; j < ``'A'` `+ (``2` `* n) - ``1``; j++) ``            ``{``              ``if` `(j >= (``'A'` `+ n - ``1``) + i)``                ``System.out.print((``char``)((``'A'` `+ n - ``1``) - ``                                 ``(j % (``'A'` `+ n - ``1``))));``                ``else` `if` `(j <= (``'A'` `+ n - ``1``) - i)``                    ``System.out.print((``char``)j);``                ``else``                    ``System.out.print(``" "``);``            ``}``            ``System.out.println();``        ``}``    ``}``     ` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `n = ``6``;``        ``ReverseCharBridge(n);``    ``}``}`` ` `/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `# Python3 code to print reverse ``# character bridge pattern`` ` `# Function to print pattern``def` `ReverseCharBridge( n ):``    ``for` `i ``in` `range``( n ):``        ``for` `j ``in` `range``( ``ord``(``'A'``), ``ord``(``'A'``) ``+` `                              ``(``2` `*` `n) ``-` `1``):``            ``if` `j >``=` `(``ord``( ``'A'` `) ``+` `n ``-` `1``) ``+` `i:``                ``print``(``chr``((``ord``(``'A'``) ``+` `n ``-` `1``) ``-` `                  ``(j ``%` `(``ord``(``'A'``) ``+` `n ``-` `1``))), end ``=` `'')``             ` `            ``elif` `j <``=` `(``ord``(``'A'``) ``+` `n ``-` `1``) ``-` `i:``                ``print``(``chr``(j), end ``=` `'')``            ``else``:``                ``print``(end ``=` `" "``)``        ``print``(``"\n"``, end ``=` `'')``         ` `# Driver Code``n ``=` `6``ReverseCharBridge(n)`` ` `# This code is contributed by "Sharad_Bhardwaj".`

## C#

 `// C# program to print reverse``// character bridge pattern``using` `System;`` ` `class` `GFG {`` ` `    ``// Function to print pattern``    ``static` `void` `ReverseCharBridge(``int` `n)``    ``{``        ``for` `(``int` `i = 0; i < n; i++) ``        ``{``            ``for` `(``int` `j = ``'A'``; j < ``'A'` `+ (2 * n) - 1; j++)``            ``{``                ``if` `(j >= (``'A'` `+ n - 1) + i)``                    ``Console.Write((``char``)((``'A'` `+ n - 1)``                    ``- (j % (``'A'` `+ n - 1))));``                 ` `                ``else` `if` `(j <= (``'A'` `+ n - 1) - i)``                    ``Console.Write((``char``)j);``                 ` `                ``else``                    ``Console.Write(``" "``);``            ``}``            ``Console.WriteLine();``        ``}``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `n = 6;``        ``ReverseCharBridge(n);``    ``}``}`` ` `// This code is contributed by vt_m.`

## PHP

 `= (65 + ``\$n` `- 1) + ``\$i``)``                ``echo` `chr``((65 + ``\$n` `- 1) - ``                    ``(``\$j` `% (65 + ``\$n` `- 1)));``                     ` `            ``else` `if` `(``\$j` `<= (65 + ``\$n` `- 1) - ``\$i``)``                ``echo` `chr``(``\$j``);``            ``else``                ``echo` `" "``;``        ``}``        ``echo` `"\n"``;``    ``}``}`` ` `// Driver Code``\$n` `= 6;``ReverseCharBridge(``\$n``);`` ` `// This code is contributed by mits ``?>`

Output :
```ABCDEFEDCBA
ABCDE EDCBA
ABCD   DCBA
ABC     CBA
AB       BA
A         A```
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up