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
- 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 <iostream> 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
<?php // PHP program to print reverse // character bridge pattern // Function to print pattern function ReverseCharBridge( $n )
{ //Ascii of A is 65
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 65; $j < 65 +
(2 * $n ) - 1; $j ++)
{
if ( $j >= (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 ?> |
Javascript
<script> // Javascript program to print reverse character bridge pattern // Function to print pattern function ReverseCharBridge(n)
{ for (let i = 0; i < n; i++)
{
for (let j = 65; j < 65 + (2 * n) - 1; j++)
{
if (j >= (65 + n - 1) + i)
document.write(String.fromCharCode((65 + n - 1) -
(j % (65 + n - 1))));
else if (j <= (65 + n - 1) - i)
document.write(String.fromCharCode(j));
else
document.write( " " );
}
document.write( "\n" );
}
} // Driver Code let n = 6; ReverseCharBridge(n); // This code is contributed by Samim Hossain Mondal. </script> |
Output
ABCDEFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A
Time Complexity: O(n2)
Auxiliary Space: O(1)