Print A pattern

Print A pattern

Here given code implementation process.

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

//include space 
void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
//include star 
void display_star(int size)
{

  for (int i = 0; i < size; i+=2)
  {
    printf(" *");
  }
}

void show_A(int size)
{
  if(size <= 3 )
  {
    return;
  }
  printf("Size : %d\n\n",size );

  //Display A pattern of given size
  for (int i = 0; i < size; i++)
  {
   
    space(size-i);

    printf("*");

    if(i==size/2)
    {
      //print middle layer
      display_star(size-1);
    }
    else if(i!=0)
    {
      space((i-1)*2);

      printf(" *");
    }
    printf("\n");

  }
 
  printf("\n");
}

int main()
{
  //Test Case
  show_A(6);
  show_A(7);
  show_A(5);
  show_A(9);
  return 0;
}

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

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

using namespace std;
class MyPattern {
	public:

    //include space 
    void space(int size) {
      for (int i = 0; i < size; ++i) {
        cout << " ";
      }
    }
	//include star 
	void display_star(int size) {
		for (int i = 0; i < size; i += 2) {
			cout << " *";
		}
	}
	void show_A(int size) {
		if (size <= 3) {
			return;
		}
		cout << "Size : " << size << "\n\n";
		int side = ((size - 1) *2);
		//Display A pattern of given size

		for (int i = 0; i < size; i++) {
			this->space(size - i);
			cout << "*";
			if (i == size / 2) {
				//print middle layer
				this->display_star(size - 1);
			} else
			if (i != 0) {
				this->space((i - 1) *2);
				cout << " *";
			}
			cout << "\n";
		}
		cout << "\n";
	}
};
int main() {
	MyPattern obj = MyPattern();
	//Test Case
	obj.show_A(6);
	obj.show_A(7);
	obj.show_A(5);
	obj.show_A(9);
	return 0;
}

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

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

  //include space 
  public void space(int size) 
  {
    for (int i = 0; i < size; ++i) 
    {
      System.out.print(" ");
    }
  }
  //include star 
  public void display_star(int size) 
  {
    for (int i = 0; i < size; i += 2) 
    {
      System.out.print(" *");
    }
  }
  public void show_A(int size) 
  {
    if (size <= 3) 
    {
      return;
    }
    System.out.print("Size : "+size+"\n\n" );
    int side = ((size - 1)* 2);
    //Display A pattern of given size

    for (int i = 0; i < size; i++) 
    {
      space(size - i);

      System.out.print("*");
      if (i == size/2) 
      {
        //print middle layer
        display_star(size - 1);
      } 
      else if (i != 0) 
      {
        space((i - 1)* 2);
        System.out.print(" *");
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args) 
  {
    MyPattern obj = new MyPattern();
    //Test Case
    obj.show_A(6);
    obj.show_A(7);
    obj.show_A(5);
    obj.show_A(9);
  }
}

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

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

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

         *
        * *
       *   *
      *     *
     * * * * *
    *         *
   *           *
  *             *
 *               *
<?php
// Php Program
// Display A pattern 
class MyPattern {
	//include space 
	public 	function space($size) {
		for ($i = 0; $i < $size; ++$i) {
			echo(" ");
		}
	}
	//include star 
	public 	function display_star($size) {
		for ($i = 0; $i < $size; $i += 2) {
			echo(" *");
		}
	}
	public 	function show_A($size) {
		if ($size <= 3) {
			return;
		}
		echo("Size : ". $size ."\n\n");
		//Display A pattern of given size
		for ($i = 0; $i < $size; $i++) {
			$this->space($size - $i);
			echo("*");
			if ($i == intval($size / 2)) {
				//print middle layer
				$this->display_star($size - 1);
			} else
			if ($i != 0) {
				$this->space(($i - 1) *2);
				echo(" *");
			}
			echo("\n");
		}
		echo("\n");
	}
}

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

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

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

	//include star 
	display_star(size) {
		for (var i = 0; i < size; i += 2) {
			process.stdout.write(" *");
		}
	}
	show_A(size) {
		if (size <= 3) {
			return;
		}

		process.stdout.write("Size : " + size + "\n\n");
		//Display A pattern of given size

		for (var i = 0; i < size; i++) {
			this.space(size - i);
			process.stdout.write("*");
			if (i == parseInt(size / 2)) {
				//print middle layer
				this.display_star(size - 1);
			} else
			if (i != 0) {
				this.space((i - 1) *2);
				process.stdout.write(" *");
			}

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

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

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

main();

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

         *
        * *
       *   *
      *     *
     * * * * *
    *         *
   *           *
  *             *
 *               *
#  Python 3 Program
#  Display A pattern 
class MyPattern :
	# include space 
	def space(self, size) :
		i = 0
		while (i < size) :
			print(" ", end = "")
			i += 1
		
	
	# include star 
	def display_star(self, size) :
		i = 0
		while (i < size) :
			print(" *", end = "")
			i += 2
		
	
	def show_A(self, size) :
		if (size <= 3) :
			return
		
		print("Size : ", size ,"\n\n", end = "")
		i = 0
		while (i < size) :
			self.space(size - i)
			print("*", end = "")
			if (i == int(size / 2)) :
				self.display_star(size - 1)
			elif (i != 0) :
				self.space((i - 1) * 2)
				print(" *", end = "")
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MyPattern()
	obj.show_A(6)
	obj.show_A(7)
	obj.show_A(5)
	obj.show_A(9)


if __name__ == "__main__":
	main()

Output

Size :  6

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

Size :  7

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

Size :  5

     *
    * *
   * * *
  *     *
 *       *

Size :  9

         *
        * *
       *   *
      *     *
     * * * * *
    *         *
   *           *
  *             *
 *               *
#  Ruby Program
#  Display A pattern 
class MyPattern 
	# include space 
	def space(size) 
		i = 0
		while (i < size) 
			print(" ")
			i += 1
		end
	end
	# include star 
	def display_star(size) 
		i = 0
		while (i < size) 
			print(" *")
			i += 2
		end
	end
	def show_A(size) 
		if (size <= 3) 
			return
		end
		print("Size  :", size ,"\n\n")
		i = 0
		while (i < size) 
			self.space(size - i)
			print("*")
			if (i == size / 2) 
				self.display_star(size - 1)
			elsif (i != 0) 
				self.space((i - 1) * 2)
				print(" *")
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main() 
	obj = MyPattern.new()
	obj.show_A(6)
	obj.show_A(7)
	obj.show_A(5)
	obj.show_A(9)
end
main()

Output

Size  :6

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

Size  :7

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

Size  :5

     *
    * *
   * * *
  *     *
 *       *

Size  :9

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

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

			if (i == (size / 2).toInt) {
				display_star(size - 1);
			} else
			if (i != 0) {
				space((i - 1) * 2);
				print(" *");
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		obj.show_A(6);
		obj.show_A(7);
		obj.show_A(5);
		obj.show_A(9);
	}
}

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

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

Output

Size :  6

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

Size :  7

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

Size :  5

     *
    * *
   * * *
  *     *
 *       *

Size :  9

         *
        * *
       *   *
      *     *
     * * * * *
    *         *
   *           *
  *             *
 *               *
// Rust Program
// Display A pattern 
fn main() {
	//Test Case
	show_a(6);
	show_a(7);
	show_a(5);
	show_a(9);
}
fn show_a(size: i32) {
	if size <= 3 {
		return;
	}
	print!("Size : {}\n\n", size);

	let mut i: i32 = 0;
	//Display A pattern of given size

	while i < size {
		space(size - i);
		print!("*");
		if i == size / 2 {
			//print middle layer
			display_star(size - 1);
		}
		else
		if i != 0 {
			space((i - 1) * 2);
			print!(" *");
		}
		print!("\n");
		i += 1;
	}
	print!("\n");
}
fn display_star(size: i32) {
	let mut i: i32 = 0;
	while i < size {
		print!(" *");
		i += 2;
	}
}
fn space(size: i32) {
	let mut i: i32 = 0;
	while i < size {
		print!(" ");
		i += 1;
	}
}

Output

Size : 6

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

Size : 7

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

Size : 5

     *
    * *
   * * *
  *     *
 *       *

Size : 9

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

Display Inverted 'A' pattern

// C Program
// Display Inverted 'A' pattern 
#include <stdio.h>

//include space 
void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
//include star 
void display_star(int size)
{

  for (int i = 0; i < size; i+=2)
  {
    printf(" *");
  }
}

void show_A(int size)
{
  if(size <= 3 )
  {
    return;
  }
  printf("Size : %d\n\n",size );

  int flag = 0;

  if(size%2==0)
  {
    flag=1;
  }

  //Display A pattern of given size
  for (int i = 0; i < size; i++)
  {
   
   
    space(i);

    printf("*");
    
    if(i != 0 && i+flag == size/2)
    {
      //print middle layer
      display_star(size-1);
    }
    else if(i != size-1)
    {
      space(((size-2)*2)-((i)*2));

      printf(" *");
    }
    printf("\n");

  }
 
  printf("\n");
}

int main()
{
  //Test Case
  show_A(6);
  show_A(7);
  show_A(5);
  show_A(9);
  return 0;
}

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
// C++ Program
// Display Inverted 'A' pattern 
#include<iostream>

using namespace std;
class MyPattern {
	public:

    //include star 
    void display_star(int size) {
      for (int i = 0; i < size; i += 2) {
        cout << " *";
      }
    }
	//include space 
	void space(int size) {
		for (int i = 0; i < size; i++) {
			cout << " ";
		}
	}
	void show_pattern(int size) {
		if (size <= 3) {
			return;
		}
		cout << "Size : " << size << "\n\n";
		int flag = 0;
		if (size % 2 == 0) {
			flag = 1;
		}
		//Display A pattern of given size

		for (int i = 0; i < size; i++) {
			this->space(i);
			cout << "*";
			if (i != 0 &&
				i + flag == size / 2) {
				//print middle layer
				this->display_star(size - 1);
			} else
			if (i != size - 1) {
				this->space(((size - 2) *2) - ((i) *2));
				cout << " *";
			}
			cout << "\n";
		}
		cout << "\n";
	}
};
int main() {
	MyPattern obj =  MyPattern();
	//Test Case
	obj.show_pattern(6);
	obj.show_pattern(7);
	obj.show_pattern(5);
	obj.show_pattern(9);
	return 0;
}

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
// C# Program
// Display Inverted 'A' pattern 
using System;
public class MyPattern {
	//include star 
	public void display_star(int size) {
		for (int i = 0; i < size; i += 2) {
			Console.Write(" *");
		}
	}
	//include space 
	public void space(int size) {
		for (int i = 0; i < size; i++) {
			Console.Write(" ");
		}
	}
	public void show_pattern(int size) {
		if (size <= 3) {
			return;
		}
		Console.Write("Size : " + size + "\n\n");
		int flag = 0;
		if (size % 2 == 0) {
			flag = 1;
		}
		//Display A pattern of given size

		for (int i = 0; i < size; i++) {
			space(i);
			Console.Write("*");
			if (i != 0 &&
				i + flag == size / 2) {
				display_star(size - 1);
			} else
			if (i != size - 1) {
				space(((size - 2) * 2) - ((i) * 2));
				Console.Write(" *");
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args) {
		MyPattern obj = new MyPattern();
		obj.show_pattern(6);
		obj.show_pattern(7);
		obj.show_pattern(5);
		obj.show_pattern(9);
	}
}

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
// Java Program
// Display Inverted 'A' pattern 
class MyPattern {
  //include star 
  public void display_star(int size) 
  {
    for (int i = 0; i < size; i += 2) 
    {
      System.out.print(" *");
    }
  }
  //include space 
  public void space(int size) 
  {
    for (int i = 0; i < size; i++) 
    {
      System.out.print(" ");
    }
  }
  public void show_pattern(int size) 
  {
    if (size <= 3) 
    {
      return;
    }
    System.out.print("Size : "+size+"\n\n");
    int flag = 0;
    if (size % 2 == 0) 
    {
      flag = 1;
    }
    //Display A pattern of given size

    for (int i = 0; i < size; i++) 
    {
      space(i);

      System.out.print("*");

      if (i != 0 && i + flag == size/2) 
      {
        //print middle layer
        display_star(size - 1);
      } 
      else if (i != size - 1) 
      {
        space(((size - 2) * 2) - ((i) * 2));

        System.out.print(" *");
      }

      System.out.print("\n");
    }

    System.out.print("\n");
  }
  public static void main(String[] args) {
    MyPattern obj = new MyPattern();
    //Test Case
    obj.show_pattern(6);
    obj.show_pattern(7);
    obj.show_pattern(5);
    obj.show_pattern(9);

  }
}

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
<?php
// Php Program
// Display Inverted 'A' pattern 
class MyPattern {
	//include star 
	public 	function display_star($size) {
		for ($i = 0; $i < $size; $i += 2) {
			echo(" *");
		}
	}
	//include space 
	public 	function space($size) {
		for ($i = 0; $i < $size; $i++) {
			echo(" ");
		}
	}
	public 	function show_pattern($size) {
		if ($size <= 3) {
			return;
		}
		echo("Size : ". $size ."\n\n");
		$flag = 0;
		if ($size % 2 == 0) {
			$flag = 1;
		}
		//Display A pattern of given size

		for ($i = 0; $i < $size; $i++) {
			$this->space($i);
			echo("*");
			if ($i != 0 &&
				$i + $flag == intval($size / 2)) {
				//print middle layer
				$this->display_star($size - 1);
			} else
			if ($i != $size - 1) {
				$this->space((($size - 2) *2) - (($i) *2));
				echo(" *");
			}
			echo("\n");
		}
		echo("\n");
	}
}

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

}
main();

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
// Node Js Program
// Display Inverted 'A' pattern 
class MyPattern {
	//include star 
	display_star(size) {
		for (var i = 0; i < size; i += 2) {
			process.stdout.write(" *");
		}
	}

	//include space 
	space(size) {
		for (var i = 0; i < size; i++) {
			process.stdout.write(" ");
		}
	}
	show_pattern(size) {
		if (size <= 3) {
			return;
		}

		process.stdout.write("Size : " + size + "\n\n");
		var flag = 0;
		if (size % 2 == 0) {
			flag = 1;
		}

		//Display A pattern of given size

		for (var i = 0; i < size; i++) {
			this.space(i);
			process.stdout.write("*");
			if (i != 0 &&
				i + flag == parseInt(size / 2)) {
				//print middle layer
				this.display_star(size - 1);
			} else
			if (i != size - 1) {
				this.space(((size - 2) *2) - ((i) *2));
				process.stdout.write(" *");
			}

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

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

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

main();

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
#  Python 3 Program
#  Display Inverted 'A' pattern 
class MyPattern :
	# include star 
	def display_star(self, size) :
		i = 0
		while (i < size) :
			print(" *", end = "")
			i += 2
		
	
	# include space 
	def space(self, size) :
		i = 0
		while (i < size) :
			print(" ", end = "")
			i += 1
		
	
	def show_pattern(self, size) :
		if (size <= 3) :
			return
		
		print("Size : ", size ,"\n\n", end = "")
		flag = 0
		if (size % 2 == 0) :
			flag = 1
		
		# Display A pattern of given size
		i = 0
		while (i < size) :
			self.space(i)
			print("*", end = "")
			if (i != 0 and i + flag == int(size / 2)) :
				self.display_star(size - 1)
			elif (i != size - 1) :
				self.space(((size - 2) * 2) - ((i) * 2))
				print(" *", end = "")
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MyPattern()
	obj.show_pattern(6)
	obj.show_pattern(7)
	obj.show_pattern(5)
	obj.show_pattern(9)


if __name__ == "__main__":
	main()

Output

Size :  6

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

Size :  7

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

Size :  5

*       *
 *     *
  * * *
   * *
    *

Size :  9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
#  Ruby Program
#  Display Inverted 'A' pattern 
class MyPattern 
	# include star 
	def display_star(size) 
		i = 0
		while (i < size) 
			print(" *")
			i += 2
		end
	end
	# include space 
	def space(size) 
		i = 0
		while (i < size) 
			print(" ")
			i += 1
		end
	end
	def show_pattern(size) 
		if (size <= 3) 
			return
		end
		print("Size  : ", size ,"\n\n")
		flag = 0
		if (size % 2 == 0) 
			flag = 1
		end
		# Display A pattern of given size
		i = 0
		while (i < size) 
			self.space(i)
			print("*")
			if (i != 0 &&
				i + flag == size / 2) 
				self.display_star(size - 1)
			elsif (i != size - 1) 
				self.space(((size - 2) * 2) - ((i) * 2))
				print(" *")
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main() 
	obj = MyPattern.new()
	obj.show_pattern(6)
	obj.show_pattern(7)
	obj.show_pattern(5)
	obj.show_pattern(9)
end
main()

Output

Size  : 6

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

Size  : 7

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

Size  : 5

*       *
 *     *
  * * *
   * *
    *

Size  : 9

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

// Scala Program
// Display Inverted 'A' pattern 
class MyPattern {
	//include star 
	def display_star(size: Int): Unit = {
		var i: Int = 0;
		while (i < size) {
			print(" *");
			i += 2;
		}
	}
	//include space 
	def space(size: Int): Unit = {
		var i: Int = 0;
		while (i < size) {
			print(" ");
			i += 1;
		}
	}
	def show_pattern(size: Int): Unit = {
		if (size <= 3) {
			return;
		}
		print("Size : " + size + "\n\n");
		var flag: Int = 0;

		if (size % 2 == 0) {
			flag = 1;
		}
		//Display A pattern of given size
		var i: Int = 0;
		while (i < size) {
			space(i);
			print("*");

			if (i != 0 &&
				i + flag == (size / 2).toInt) {
				display_star(size - 1);
			} else
			if (i != size - 1) {
				space(((size - 2) * 2) - ((i) * 2));
				print(" *");
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		obj.show_pattern(6);
		obj.show_pattern(7);
		obj.show_pattern(5);
		obj.show_pattern(9);
	}
}

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
// Swift Program
// Display Inverted "A" pattern 
class MyPattern {
	//include star 
	func display_star(_ size: Int) {
		var i: Int = 0;
		while (i < size) {
			print(" *", terminator: "");
			i += 2;
		}
	}
	//include space 
	func space(_ size: Int) {
		var i: Int = 0;
		while (i < size) {
			print(" ", terminator: "");
			i += 1;
		}
	}
	func show_pattern(_ size: Int) {
		if (size <= 3) {
			return;
		}
		print("Size : ", size ,"\n\n", terminator: "");
		var flag: Int = 0;
		if (size % 2 == 0) {
			flag = 1;
		}
		//Display A pattern of given size
		var i: Int = 0;
		while (i < size) {
			self.space(i);
			print("*", terminator: "");
			if (i != 0 &&
				i + flag == size / 2) {
				self.display_star(size - 1);
			} else
			if (i != size - 1) {
				self.space(((size - 2) * 2) - ((i) * 2));
				print(" *", terminator: "");
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main() {
	let obj: MyPattern = MyPattern();
	obj.show_pattern(6);
	obj.show_pattern(7);
	obj.show_pattern(5);
	obj.show_pattern(9);
}
main();

Output

Size :  6

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

Size :  7

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

Size :  5

*       *
 *     *
  * * *
   * *
    *

Size :  9

*               *
 *             *
  *           *
   *         *
    * * * * *
     *     *
      *   *
       * *
        *
fn main(){
	//Test Case
	show_a(6);
	show_a(7);
	show_a(5);
	show_a(9);
	
}
fn show_a(size: i32) {
	if size <= 3 {
		return;
	}
	print!("Size : {}\n\n", size);
	let mut flag: i32 = 0;
	if size % 2 == 0 {
		flag = 1;
	}
	let mut i: i32 = 0;
	//Display A pattern of given size

	while i < size {
		space(i);
		print!("*");
		if i != 0 && i + flag == size / 2 {
			//print middle layer
			display_star(size - 1);
		}
		else
		if i != size - 1 {
			space(((size - 2) * 2) - ((i) * 2));
			print!(" *");
		}
		print!("\n");
		i += 1;
	}
	print!("\n");
}
fn display_star(size: i32) {
	let mut i: i32 = 0;
	while i < size {
		print!(" *");
		i += 2;
	}
}
fn space(size: i32) {
	let mut i: i32 = 0;
	while i < size {
		print!(" ");
		i += 1;
	}
}

Output

Size : 6

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

Size : 7

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

Size : 5

*       *
 *     *
  * * *
   * *
    *

Size : 9

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


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







© 2021, kalkicode.com, All rights reserved