Skip to main content

Print V pattern

In this article, we will discuss a C program to display a V pattern using asterisks (*) and spaces. The program takes an input size parameter and generates the V pattern based on that size.

Problem Statement

The problem is to print a V pattern using asterisks. The size of the pattern determines the number of rows in the pattern. The V pattern consists of lines that form a V shape, where each line is composed of asterisks (*) and spaces. The pattern starts with a single asterisk at the top and expands downwards, forming the V shape.

The program should accept an integer size as input and generate the V pattern accordingly. For example, if the size is 5, the output would be:

*       *
 *     *
  *   *
   * *
    *

Algorithm

To generate the V pattern, we can follow the following algorithm:

  1. Define a function space(int size) that takes the size parameter and prints the required number of spaces.
  2. Define a function show_v(int size) that takes the size parameter and generates the V pattern.
  3. In the show_v function, check if the size is less than 2. If so, return as the pattern cannot be generated.
  4. Initialize a variable side with a value of (size * 2) - 3. This variable represents the number of spaces on each side of the V shape.
  5. Use a loop to iterate through each row of the pattern:
    • Print the required number of spaces using the space function.
    • Print an asterisk (*) to represent the left side of the V shape.
    • Print the required number of spaces using the space function, considering the decreasing number of spaces on each side.
    • If the current row is within the range of side, print an asterisk (*) to represent the right side of the V shape.
    • Decrement the side variable.
    • Print a new line character to move to the next row.

Pseudocode


function space(size):
    for i from 0 to size-1:
        print " "

function show_v(size):
    if size < 2:
        return
    side = (size * 2) - 3
    for i from 0 to size-1:
        space(i)
        print "*"
        space(side-i)
        if i < side:
            print "*"
        side--
        print newline

function main():
    show_v(5)
    show_v(7)
    show_v(3)
    show_v(9)

The pseudocode above outlines the functions and main logic for generating the V pattern. Here's a breakdown of each function:

  1. space(size): This function takes an input size and prints the corresponding number of spaces. It uses a loop to iterate from 0 to size-1 and prints a space in each iteration.

  2. show_v(size): This function generates the V pattern based on the input size. It first checks if the size is less than 2, in which case it returns since the pattern cannot be generated. It then initializes a variable side to (size * 2) - 3, which represents the number of spaces on each side of the V shape.

    The function uses a loop to iterate from 0 to size-1, representing each row of the pattern. In each iteration, it calls the space function with the current row number to print the corresponding number of spaces. It then prints an asterisk to represent the left side of the V shape.

    Next, it calls the space function again, but this time with side-i as the input, to print the decreasing number of spaces on each side of the V shape. If the current row is within the range of side, it prints an asterisk to represent the right side of the V shape.

    After that, it decrements the side variable and prints a newline character to move to the next row.

  3. main(): This is the main function that calls the show_v function with different input sizes to generate and display the V pattern. In the provided example, it calls show_v with sizes 5, 7, 3, and 9.

Code Solution

// C Program
// Display V pattern 
#include <stdio.h>

//include space 
void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
void show_v(int size)
{
  if(size < 2 )
  {
    return;
  }
  printf("Size : %d\n\n",size );

  int side = (size*2)-3;


  for (int i = 0; i < size; i++)
  {
    space(i);
    printf("*");
    space(side-i);
    if(i < side)
    {
      printf("*");
    }
    side--;
    printf("\n");
  }
 
  printf("\n");
}

int main()
{
  //Test Case
  show_v(5);
  show_v(7);
  show_v(3);
  show_v(9);
  return 0;
}

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
// C++ Program
// Display V pattern 
#include<iostream>

using namespace std;
class MyPattern {
  public:

    //include space 
    void space(int size) {
      for (int i = 0; i < size; ++i) {
        cout << " ";
      }
    }
  void show_v(int size) {
    if (size < 2) {
      return;
    }
    cout << "Size : " << size << "\n\n";
    int side = (size *2) - 3;

    for (int i = 0; i < size; i++) {
      this->space(i);
      cout << "*";
      this->space(side - i);
      if (i < side) {
        cout << "*";
      }
      side--;
      cout << "\n";
    }
    cout << "\n";
  }
};
int main() {
  MyPattern obj = MyPattern();
  //Test Case
  obj.show_v(5);
  obj.show_v(7);
  obj.show_v(3);
  obj.show_v(9);
  return 0;
}

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
// Java Program
// Display V pattern 
class MyPattern {

  //include space 
  public void space(int size) {
    for (int i = 0; i < size; ++i) {
      System.out.print(" ");
    }
  }
  public void show_v(int size) {
    if (size < 2) {
      return;
    }
    System.out.print("Size : "+size+"\n\n");
    int side = (size*2) - 3;
   
    for (int i = 0; i < size; i++) {
      space(i);
      System.out.print("*");
      space(side - i);
      if (i < side) {
        System.out.print("*");
      }
      side--;
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args) 
  {
    MyPattern obj = new MyPattern();
    //Test Case
    obj.show_v(5);
    obj.show_v(7);
    obj.show_v(3);
    obj.show_v(9);
  }
}

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
// C# Program
// Display V pattern 
using System;
public class MyPattern {
  //include space 
  public void space(int size) {
    for (int i = 0; i < size; ++i) {
      Console.Write(" ");
    }
  }
  public void show_v(int size) {
    if (size < 2) {
      return;
    }
    Console.Write("Size : " + size + "\n\n");
    int side = (size * 2) - 3;
    for (int i = 0; i < size; i++) {
      space(i);
      Console.Write("*");
      space(side - i);
      if (i < side) {
        Console.Write("*");
      }
      side--;
      Console.Write("\n");
    }
    Console.Write("\n");
  }
  public static void Main(String[] args) {
    MyPattern obj = new MyPattern();
    obj.show_v(5);
    obj.show_v(7);
    obj.show_v(3);
    obj.show_v(9);
  }
}

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
<?php
// Php Program
// Display V pattern 
class MyPattern {
  //include space 

  public  function space($size) {
    for ($i = 0; $i < $size; ++$i) {
      echo(" ");
    }
  }
  public  function show_v($size) {
    if ($size < 2) {
      return;
    }
    echo("Size : ". $size ."\n\n");
    $side = ($size *2) - 3;
    
    for ($i = 0; $i < $size; $i++) {
      $this->space($i);
      echo("*");
      $this->space($side - $i);
      if ($i < $side) {
        echo("*");
      }
      $side--;
      echo("\n");
    }
    echo("\n");
  }
}

function main() {
  $obj = new MyPattern();
  //Test Case
  $obj->show_v(5);
  $obj->show_v(7);
  $obj->show_v(3);
  $obj->show_v(9);

}
main();

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
// Node Js Program
// Display V pattern 
class MyPattern {
  //include space 
  space(size) {
    for (var i = 0; i < size; ++i) {
      process.stdout.write(" ");
    }
  }
  show_v(size) {
    if (size < 2) {
      return;
    }

    process.stdout.write("Size : " + size + "\n\n");
    var side = (size *2) - 3;
    
    for (var i = 0; i < size; i++) {
      this.space(i);
      process.stdout.write("*");
      this.space(side - i);
      if (i < side) {
        process.stdout.write("*");
      }
      side--;
      process.stdout.write("\n");
    }

    process.stdout.write("\n");
  }
}

function main(args) {
  var obj = new MyPattern();
  //Test Case
  obj.show_v(5);
  obj.show_v(7);
  obj.show_v(3);
  obj.show_v(9);
}

main();

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
#  Python 3 Program
#  Display V pattern 
class MyPattern :
  # include space 
  def space(self, size) :
    i = 0
    while (i < size) :
      print(" ", end = "")
      i += 1
    
  
  def show_v(self, size) :
    if (size < 2) :
      return
    
    print("Size : ", size ,"\n\n", end = "")
    side = (size * 2) - 3
    i = 0
    while (i < size) :
      self.space(i)
      print("*", end = "")
      self.space(side - i)
      if (i < side) :
        print("*", end = "")
      
      side -= 1
      print("\n", end = "")
      i += 1
    
    print("\n", end = "")
  

def main() :
  obj = MyPattern()
  obj.show_v(5)
  obj.show_v(7)
  obj.show_v(3)
  obj.show_v(9)


if __name__ == "__main__":
  main()

Output

Size :  5

*       *
 *     *
  *   *
   * *
    *

Size :  7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size :  3

*   *
 * *
  *

Size :  9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
#  Ruby Program
#  Display V pattern 
class MyPattern 
  # include space 
  def space(size) 
    i = 0
    while (i < size) 
      print(" ")
      i += 1
    end
  end
  def show_v(size) 
    if (size < 2) 
      return
    end
    print("Size  : ", size ,"\n\n")
    side = (size * 2) - 3
    i = 0
    while (i < size) 
      self.space(i)
      print("*")
      self.space(side - i)
      if (i < side) 
        print("*")
      end
      side -= 1
      print("\n")
      i += 1
    end
    print("\n")
  end
end
def main() 
  obj = MyPattern.new()
  obj.show_v(5)
  obj.show_v(7)
  obj.show_v(3)
  obj.show_v(9)
end
main()

Output

Size  : 5

*       *
 *     *
  *   *
   * *
    *

Size  : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size  : 3

*   *
 * *
  *

Size  : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *

// Scala Program
// Display V pattern 
class MyPattern {
  //include space 
  def space(size: Int): Unit = {
    var i: Int = 0;
    while (i < size) {
      print(" ");
      i += 1;
    }
  }
  def show_v(size: Int): Unit = {
    if (size < 2) {
      return;
    }
    print("Size : " + size + "\n\n");
    var side: Int = (size * 2) - 3;
    var i: Int = 0;
    while (i < size) {
      space(i);
      print("*");
      space(side - i);

      if (i < side) {
        print("*");
      }
      side -= 1;
      print("\n");
      i += 1;
    }
    print("\n");
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    obj.show_v(5);
    obj.show_v(7);
    obj.show_v(3);
    obj.show_v(9);
  }
}

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
// Swift Program
// Display V pattern 
class MyPattern {
  //include space 
  func space(_ size: Int) {
    var i: Int = 0;
    while (i < size) {
      print(" ", terminator: "");
      i += 1;
    }
  }
  func show_v(_ size: Int) {
    if (size < 2) {
      return;
    }
    print("Size : ", size ,"\n\n", terminator: "");
    var side: Int = (size * 2) - 3;
    var i: Int = 0;
    while (i < size) {
      self.space(i);
      print("*", terminator: "");
      self.space(side - i);
      if (i < side) {
        print("*", terminator: "");
      }
      side -= 1;
      print("\n", terminator: "");
      i += 1;
    }
    print("\n", terminator: "");
  }
}
func main() {
  let obj: MyPattern = MyPattern();
  obj.show_v(5);
  obj.show_v(7);
  obj.show_v(3);
  obj.show_v(9);
}
main();

Output

Size :  5

*       *
 *     *
  *   *
   * *
    *

Size :  7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size :  3

*   *
 * *
  *

Size :  9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *
fn main() {
  //Test Case
  show_v(5);
  show_v(7);
  show_v(3);
  show_v(9);
}
fn show_v(size: i32) {
  if size < 2 {
    return;
  }
  print!("Size : {}\n\n", size);
  let mut side: i32 = (size * 2) - 3;
  let mut i: i32 = 0;
  while i < size {
    space(i);
    print!("*");
    space(side - i);
    if i < side {
      print!("*");
    }
    side -= 1;
    print!("\n");
    i += 1;
  }
  print!("\n");
}
fn space(size: i32) {
  let mut i: i32 = 0;
  while i < size {
    print!(" ");
    i += 1;
  }
}

Output

Size : 5

*       *
 *     *
  *   *
   * *
    *

Size : 7

*           *
 *         *
  *       *
   *     *
    *   *
     * *
      *

Size : 3

*   *
 * *
  *

Size : 9

*               *
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *

Another inverted V pattern

// C Program
// Display inverted v pattern 
#include <stdio.h>

//include space 
void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
void print_inverted_v(int size)
{
  if (size <= 2)
  {
    return;
  }
  printf("Size : %d\n\n", size);
  for (int i = 0; i < size; i++)
  {
    space(size - i);
    printf("*");
    space(i + i - 1);
    if (i != 0)
    {
      printf("*");
    }
    printf("\n");
  }
  printf("\n");
}
int main()
{
  //Test Case
  print_inverted_v(5);
  print_inverted_v(7);
  print_inverted_v(3);
  print_inverted_v(9);
  return 0;
}

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
// Java Program
// Display inverted v pattern 
class MyPattern
{
  //include space 
  public void space(int size)
  {
    for (int i = 0; i < size; ++i)
    {
      System.out.print(" ");
    }
  }
  public void print_inverted_v(int size)
  {
    if (size <= 2)
    {
      return;
    }
    System.out.print("Size : " + size + "\n\n");
    for (int i = 0; i < size; i++)
    {
      space(size - i);
      System.out.print("*");
      space(i + i - 1);
      if (i != 0)
      {
        System.out.print("*");
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Case
    obj.print_inverted_v(5);
    obj.print_inverted_v(7);
    obj.print_inverted_v(3);
    obj.print_inverted_v(9);
  }
}

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
// C++ Program
// Display inverted v pattern 
#include<iostream>

using namespace std;
class MyPattern
{
  public:
    //include space 
    void space(int size)
    {
      for (int i = 0; i < size; ++i)
      {
        cout << " ";
      }
    }
  void print_inverted_v(int size)
  {
    if (size <= 2)
    {
      return;
    }
    cout << "Size : " << size << "\n\n";
    for (int i = 0; i < size; i++)
    {
      this->space(size - i);
      cout << "*";
      this->space(i + i - 1);
      if (i != 0)
      {
        cout << "*";
      }
      cout << "\n";
    }
    cout << "\n";
  }
};
int main()
{
  MyPattern obj =  MyPattern();
  //Test Case
  obj.print_inverted_v(5);
  obj.print_inverted_v(7);
  obj.print_inverted_v(3);
  obj.print_inverted_v(9);
  return 0;
}

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
// C# Program
// Display inverted v pattern 
using System;
class MyPattern
{
  //include space 
  public void space(int size)
  {
    for (int i = 0; i < size; i++)
    {
      Console.Write(" ");
    }
  }
  public void print_inverted_v(int size)
  {
    if (size <= 2)
    {
      return;
    }
    Console.Write("Size : " + size + "\n\n");
    for (int i = 0; i < size; i++)
    {
      space(size - i);
      Console.Write("*");
      space(i + i - 1);
      if (i != 0)
      {
        Console.Write("*");
      }
      Console.Write("\n");
    }
    Console.Write("\n");
  }
  public static void Main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Case
    obj.print_inverted_v(5);
    obj.print_inverted_v(7);
    obj.print_inverted_v(3);
    obj.print_inverted_v(9);
  }
}

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
<?php
// Php Program
// Display inverted v pattern 
class MyPattern
{
  //include space 
  public  function space($size)
  {
    for ($i = 0; $i < $size; ++$i)
    {
      echo(" ");
    }
  }
  public  function print_inverted_v($size)
  {
    if ($size <= 2)
    {
      return;
    }
    echo("Size : ". $size ."\n\n");
    for ($i = 0; $i < $size; $i++)
    {
      $this->space($size - $i);
      echo("*");
      $this->space($i + $i - 1);
      if ($i != 0)
      {
        echo("*");
      }
      echo("\n");
    }
    echo("\n");
  }
}

function main()
{
  $obj = new MyPattern();
  //Test Case
  $obj->print_inverted_v(5);
  $obj->print_inverted_v(7);
  $obj->print_inverted_v(3);
  $obj->print_inverted_v(9);
}
main();

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
// Node Js Program
// Display inverted v pattern 
class MyPattern
{
  //include space 
  space(size)
  {
    for (var i = 0; i < size; ++i)
    {
      process.stdout.write(" ");
    }
  }
  print_inverted_v(size)
  {
    if (size <= 2)
    {
      return;
    }
    process.stdout.write("Size : " + size + "\n\n");
    for (var i = 0; i < size; i++)
    {
      this.space(size - i);
      process.stdout.write("*");
      this.space(i + i - 1);
      if (i != 0)
      {
        process.stdout.write("*");
      }
      process.stdout.write("\n");
    }
    process.stdout.write("\n");
  }
}

function main(args)
{
  var obj = new MyPattern();
  //Test Case
  obj.print_inverted_v(5);
  obj.print_inverted_v(7);
  obj.print_inverted_v(3);
  obj.print_inverted_v(9);
}
main();

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
#  Python 3 Program
#  Display inverted v pattern 
class MyPattern :
  # include space 
  def space(self, size) :
    i = 0
    while (i < size) :
      print(end = " ")
      i += 1
    
  
  def print_inverted_v(self, size) :
    if (size <= 2) :
      return
    
    print("Size : ", size ,"\n\n", end = "")
    i = 0
    while (i < size) :
      self.space(size - i)
      print(end = "*")
      self.space(i + i - 1)
      if (i != 0) :
        print(end = "*")
      
      print(end = "\n")
      i += 1
    
    print(end = "\n")
  

def main() :
  obj = MyPattern()
  # Test Case
  obj.print_inverted_v(5)
  obj.print_inverted_v(7)
  obj.print_inverted_v(3)
  obj.print_inverted_v(9)


if __name__ == "__main__" : main()

Output

Size :  5

     *
    * *
   *   *
  *     *
 *       *

Size :  7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size :  3

   *
  * *
 *   *

Size :  9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
#  Ruby Program
#  Display inverted v pattern 
class MyPattern

  # include space 
  def space(size)
  
    i = 0
    while (i < size)
    
      print(" ")
      i += 1
    end
  end
  def print_inverted_v(size)
  
    if (size <= 2)
    
      return
    end
    print("Size : ", size ,"\n\n")
    i = 0
    while (i < size)
    
      self.space(size - i)
      print("*")
      self.space(i + i - 1)
      if (i != 0)
      
        print("*")
      end
      print("\n")
      i += 1
    end
    print("\n")
  end
end
def main()

  obj = MyPattern.new()
  # Test Case
  obj.print_inverted_v(5)
  obj.print_inverted_v(7)
  obj.print_inverted_v(3)
  obj.print_inverted_v(9)
end
main()

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *

// Scala Program
// Display inverted v pattern 
class MyPattern
{
  //include space 
  def space(size: Int): Unit = {
    var i: Int = 0;
    while (i < size)
    {
      print(" ");
      i += 1;
    }
  }
  def print_inverted_v(size: Int): Unit = {
    if (size <= 2)
    {
      return;
    }
    print("Size : " + size + "\n\n");
    var i: Int = 0;
    while (i < size)
    {
      space(size - i);
      print("*");
      space(i + i - 1);
      if (i != 0)
      {
        print("*");
      }
      print("\n");
      i += 1;
    }
    print("\n");
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    //Test Case
    obj.print_inverted_v(5);
    obj.print_inverted_v(7);
    obj.print_inverted_v(3);
    obj.print_inverted_v(9);
  }
}

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
// Swift Program
// Display inverted v pattern 
class MyPattern
{
  //include space 
  func space(_ size: Int)
  {
    var i: Int = 0;
    while (i < size)
    {
      print(" ", terminator: "");
      i += 1;
    }
  }
  func print_inverted_v(_ size: Int)
  {
    if (size <= 2)
    {
      return;
    }
    print("Size : ", size ,"\n");
    var i: Int = 0;
    while (i < size)
    {
      self.space(size - i);
      print(terminator: "*");
      self.space(i + i - 1);
      if (i != 0)
      {
        print(terminator: "*");
      }
      print(terminator: "\n");
      i += 1;
    }
    print(terminator: "\n");
  }
}
func main()
{
  let obj: MyPattern = MyPattern();
  //Test Case
  obj.print_inverted_v(5);
  obj.print_inverted_v(7);
  obj.print_inverted_v(3);
  obj.print_inverted_v(9);
}
main();

Output

Size :  5

     *
    * *
   *   *
  *     *
 *       *

Size :  7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size :  3

   *
  * *
 *   *

Size :  9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
fn main()
{
  //Test Case
  print_inverted_v(5);
  print_inverted_v(7);
  print_inverted_v(3);
  print_inverted_v(9);
}
fn print_inverted_v(size: i32)
{
  if size <= 2
  {
    return;
  }
  print!("Size : {}\n\n", size);
  let mut i: i32 = 0;
  while i < size
  {
    space(size - i);
    print!("*");
    space(i + i - 1);
    if i != 0
    {
      print!("*");
    }
    print!("\n");
    i += 1;
  }
  print!("\n");
}
fn space(size: i32)
{
  let mut i: i32 = 0;
  while i < size
  {
    print!(" ");
    i += 1;
  }
}

Output

Size : 5

     *
    * *
   *   *
  *     *
 *       *

Size : 7

       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *

Size : 3

   *
  * *
 *   *

Size : 9

         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *

Time Complexity

The time complexity of the program is O(n^2), where n is the size parameter. This is because we have nested loops where the outer loop runs n times and the inner loop runs a decreasing number of times on each iteration. Therefore, the overall time complexity is quadratic in terms of the input size.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment