The Wayback Machine - https://web.archive.org/web/20241001054911/https://www.geeksforgeeks.org/program-for-inverted-v-pattern/
Open In App

Program to print V and inverted-V pattern

Last Updated : 17 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 <bits/stdc++.h>
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<n-1:
            print(chr(i + 65), end = '')
        print()
        k += 1
  
# Driver Code 
  
# taking size from the user 
n = 5
  
# function calling 
pattern(n)
  
# This code is contributed 
# by SamyuktaSHegde


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




<?php
// PHP Implementation to print 
// the pattern
  
// Function definition
function pattern($n)
{
    $k = 0;
    for ($i = $n - 1; $i >= 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




<script>
  
      // JavaScript Implementation to 
      // print the pattern
  
      // Function definition
      function pattern(n) {
        var i,
          j,
          k = 0;
        for (i = n - 1; i >= 0; i--) {
          // outer gap loop
          for (j = n - 1; j > k; j--) {
            document.write("  ");
          }
  
          // 65 is ASCII of 'A'
          document.write(String.fromCharCode(i + 65));
  
          // inner gap loop
          for (j = 1; j < k * 2; j++) 
          document.write("  ");
  
          if (i < n - 1) 
          document.write(String.fromCharCode(i + 65));
          document.write("<br>");
          k++;
        }
      }
  
      // Driver code
        
      // taking size from the user
      var n = 5;
        
      // function calling
      pattern(n);
        
</script>


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 <bits/stdc++.h>
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




<?php
// PHP Implementation to  
// print the pattern
  
// Function definition
function pattern($n)
{
    for ($i = $n - 1; $i >= 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




<script>
  
// JavaScript Implementation to print the pattern
      
    // Function definition
    function pattern(n)
    {
        let i, j;
        for (i = n - 1; i >= 0; i--)
        {
            // outer gap loop
            for (j = n - 1; j > i; j--)
            {
                document.write("  ");
            }
   
            // 65 is ASCII of 'A'
            document.write(String.fromCharCode(i + 65));
   
            // inner gap loop
            for (j = 1; j < (i * 2); j++)
                document.write("  ");
   
            if (i >= 1)
                document.write(String.fromCharCode(i + 65));
            document.write("<br>");
        }
    }
      
    // Driver code
      
    // taking size from the user
    let n = 5;
      
    // function calling
    pattern(n);
      
      
  
// This code is contributed by unknown2108
  
</script>


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.



Previous Article
Next Article

Similar Reads

Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Inverted Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a pattern of N rows representing an inverted full pyramid. In this pattern, the first row has (2 * N - 1) stars, the second row has (2 * N - 3) stars, and so on until the Nth row, which has only 1 star. All stars are center-aligned. Examples: Input: 3Output: ***** *** * Input: 5Output: ********* ******* ****
4 min read
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
Program to Print Butterfly Pattern (Star Pattern)
Given an integer N, print N rows of Butterfly pattern. Examples: Input: 3Output: * *** ********* *** * Input: 5Output: * *** ***** ******* ***************** ******* ***** *** * Approach: The problem can be solved using three nested loops inside an outer loop. The outer loop will run for the rows, the first inner loop will print the stars, the secon
6 min read
Program to Print Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a full pyramid pattern with N rows. In this pattern, each row contains an odd number of stars, ranging from 1 star in the first row to (2 * N - 1) stars in the Nth row. All the stars are center-aligned. Examples: Input: 3Output: * ******** Input: 5Output: * *** ***** **************** Approach: The problem ca
4 min read
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
Difference between Inverted Index and Forward Index
Inverted Index It is a data structure that stores mapping from words to documents or set of documents i.e. directs you from word to document.Steps to build Inverted index are:Fetch the document and gather all the words.Check for each word, if it is present then add reference of document to index else create new entry in index for that word.Repeat a
2 min read
Maximum path sum in an Inverted triangle | SET 2
Given numbers in form of an Inverted triangle. By starting at the bottom of the triangle and moving to adjacent numbers on the row above, find the maximum total from bottom to top.Examples: Input : 1 5 3 4 8 1 Output : 14 Input : 8 5 9 3 2 4 6 7 4 3 Output : 23 Method 1: Recursion In the previous article we saw an approach of the problem where the
13 min read
Encrypt given Array in single digit using inverted Pascal Triangle
Given an array arr[] of length N (N > 1)containing positive integers, the task is to encrypt the numbers of the array into a single digit using the inverted Pascal triangle as follows. From the starting of the array find the sum of two adjacent elements.Replace the sum with only the digit at the unit position of the sum.Replace all the array ele
11 min read
Inverted Index
An Inverted Index is a data structure used in information retrieval systems to efficiently retrieve documents or web pages containing a specific term or set of terms. In an inverted index, the index is organized by terms (words), and each term points to a list of documents or web pages that contain that term. Inverted indexes are widely used in sea
7 min read
Program to print diamond pattern using numbers and stars
Program to print the following pattern of a half diamond for N. Example: Input: N = 5Output: 1 2*3 4*5*6 7*8*9*10 11*12*13*14*15 11*12*13*14*15 7*8*9*10 4*5*6 2*3 1 This program is divided into four parts. C/C++ Code // C++ program to print the half diamond // pattern using numbers and stars #include <iostream> using namespace std; void patte
5 min read
Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement1. Hollow pyramid/triangle pattern The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row. Examples: Input: n=6 Output: # # # # # # # #
15+ min read
Program to print interesting pattern
Program to print following pattern: ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** ****5*5*5*5*5**** ***6*6*6*6*6*6*** **7*7*7*7*7*7*7** Examples : Input : 4 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** Input :5 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4**
10 min read
Program to print number pattern
We have to print a pattern where in middle column contains only 1, right side columns contain constant digit which is greater than 1 and left side columns contains constant digit which is greater than 1. Every row should look like a Palindrome. Examples : Input : 3 Output : 1 2 1 2 1 Input : 5 Output : 1 2 1 2 3 2 1 2 3 2 1 2 1 Below is the impleme
7 min read
Program to print number with star pattern
We have to print the pattern as given in the below example.Examples : Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1 C/C++ Code #include <iostream> using namespace std; // C++ program to print above pattern void display(int n) { // 'sp' used for space and 'st' used for star int sp = n
6 min read
Program to print double headed arrow pattern
Given an integer N which is the number of rows, the task is to draw the number pattern in the shape of a double headed arrow.Prerequisite: The pattern is a grow and shrink type pattern and hence basic knowledge to execute loops is required to understand the topic and the code in any language. The geometric shape can be visualized as- Examples: Inpu
9 min read
Program to print the arrow pattern
Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C/C++ Code // C++ program to print the // arrow pattern #include <bits/stdc++.h> using namespace
10 min read
Program to print the pattern "GFG"
In this article, given the value of n(length of the alphabet) and k(width of the alphabet) we will learn how to print the pattern "GFG" using stars and white-spaces. Examples: INPUT: n=7, k=5 OUTPUT: ***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * ***** INPUT: n=11, k=7 OUTPUT: ******* ******* ******* * * * * * * * * * *
8 min read
Program to print mirror image of Swastika Pattern
Given the number of rows and columns, print the mirror image of swastika pattern.Note : The number of rows and columns should be same and an odd number. This will generate a perfect mirror image of swastika pattern. Examples : Input : row = 5, col = 5 Output : * * * * * * * * * * * * * * * * * Input : row = 9, col = 9 Output : * * * * * * * * * * *
12 min read
Program to print circle pattern
Given a positive integer n i.e, the radius of the circle, print a circle using stars. Examples : Input : n = 3 Output : *** * * * * * * * * * * *** Input : n = 6 Output : ***** ** ** ** ** * * * * * * * * * * * * * * ** ** ** ** ***** Below is the implementation: C/C++ Code // C++ implementation to print circle pattern #include <bits/stdc++.h
5 min read
Program to print Crown Pattern
Given the value of length, print the crown pattern using '*' and '#'. Note : In order to print a perfect crown, take the value of length as an odd number greater than 15. Examples : Input: length = 19 Output: * * * # # # ## ### ## ### ##### ### #### ####### #### ################### ################### ################### ******************* Input:
7 min read
Program to print pattern
Given the value of n, print the following pattern.Examples : Input : n = 4 Output : A1 AB12 ABC123 ABCD1234 Input : n = 7 Output : A1 AB12 ABC123 ABCD1234 ABCDE12345 ABCDEF123456 ABCDEFG1234567 Below is the implementation to print the above pattern : C/C++ Code // C++ program to print given pattern #include <bits/stdc++.h> using namespace std
5 min read
Program to print numeric pattern | Set - 2
Given a number as 'num', and a number of lines as 'num_of_lines' where 'num' implies the starting number from which the pattern has to be started and 'num_of_lines' implies the number of lines that have to be printed. Now, according to the above information, print a pattern as given below. Examples: Input: num = 7, num_of_lines = 4 Output: 7 14 15
5 min read
Program to print Sine-Wave Pattern
Give the Height and Width of a Wave to print the Sine wave pattern Examples: Input : wave_height=5 wave_length=10 Output : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Recommended: Please try you
15 min read
Program to Print Mirror Image of Sine-Wave Pattern
A sine wave is a mathematical curve that oscillates between a maximum and minimum value, smoothly transitioning between positive and negative values. It is commonly used to represent periodic phenomena such as sound, light, and electrical signals. Examples: Give the Height and Width of a Wave to print the pattern. Input : wave_height=5 wave_length=
5 min read
Program to Print the Trapezium Pattern
Given 'num' which indicates number of lines.The task is to print a trapezium pattern in num lines.Examples: Input : 4 Output : 1*2*3*4*17*18*19*20 5*6*7*14*15*16 8*9*12*13 10*11 Input : 2 Output : 1*2*5*6 3*4 Algorithm : step 1. To read num which indicates the number of lines. step 2.We are diving the pattern into 2 halves that is LHS part and the
6 min read
Program to print the Zigzag pattern
Given a number N denoting the number of rows. The task is to print the zigzag pattern with N rows as shown in the below examples.Examples: Input : N = 3 Output : 1 3*2 4*5*6 Input : N = 5 Output : 1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 Approach: Use a for loop for printing the number of rows.Use two variables var and var1 for odd and even rows respect
6 min read
Program to print the pattern with two hollow Triangles
Given a number n, the task is to write a program to draw the following pattern using '$'. $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ Approach: The above pattern is printed by breaking it into smaller patterns: The above triangle $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $This is
15+ min read
Program to print Spiral Pattern
Given a number n as the size of the matrix, the task is to print a spiral pattern in 2D array of size n. Examples: Input: n = 5 Output: 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 Preview of the Spiral Pattern: Approach: Create a 2D array of size nStore the boundary of the array in boundary variable. Initially it will be n-1 a
10 min read