Print K pattern

Here given code implementation process.

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

//include space 
void space(int size)
{
  for (int i = 0; i < size; ++i)
  {
    printf(" ");
  }
}
void show_k(int size)
{
  if(size<2 || size%2==0)
  {
    return;
  }
  printf("Size : %d\n\n",size );
  //Display top half pattern
  for (int i = 0; i < size-2; i+=2)
  {
    
    printf("*");
    space(size-1-i);
    printf("*");
    printf("\n");

  }
  //Display bottom half pattern
  for (int i = 0; i < size; i+=2)
  {
    
    printf("*");
    space(i);
    printf("*");
    printf("\n");
  }
  printf("\n");
}

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

Output

Size : 5

*    *
*  *
**
*  *
*    *

Size : 7

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

Size : 3

*  *
**
*  *

Size : 9

*        *
*      *
*    *
*  *
**
*  *
*    *
*      *
*        *
// C++ Program
// Display k 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_k(int size) {
		if (size < 2 ||
			size % 2 == 0) {
			return;
		}
		cout << "Size : " << size << "\n\n";
		//Display top half pattern

		for (int i = 0; i < size - 2; i += 2) {
			cout << "*";
			this->space(size - 1 - i);
			cout << "*";
			cout << "\n";
		}
		//Display bottom half pattern

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

Output

Size : 5

*    *
*  *
**
*  *
*    *

Size : 7

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

Size : 3

*  *
**
*  *

Size : 9

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

  //include space 
  public void space(int size) {
    for (int i = 0; i < size; ++i) {
      System.out.print(" ");
    }
  }
  public void show_k(int size) {

    if (size < 2 || size % 2 == 0) {
      return;
    }
    System.out.print("Size : "+size+"\n\n");
    //Display top half pattern

    for (int i = 0; i < size - 2; i += 2) {
      System.out.print("*");
      space(size - 1 - i);
      System.out.print("*");
      System.out.print("\n");
    }
    //Display bottom half pattern

    for (int i = 0; i < size; i += 2) {
      System.out.print("*");
      space(i);
      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_k(5);
    obj.show_k(7);
    obj.show_k(3);
    obj.show_k(9);
  }
}

Output

Size : 5

*    *
*  *
**
*  *
*    *

Size : 7

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

Size : 3

*  *
**
*  *

Size : 9

*        *
*      *
*    *
*  *
**
*  *
*    *
*      *
*        *
// C# Program
// Display k 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_k(int size) {
		if (size < 2 ||
			size % 2 == 0) {
			return;
		}
		Console.Write("Size : " + size + "\n\n");
		//Display top half pattern

		for (int i = 0; i < size - 2; i += 2) {
			Console.Write("*");
			space(size - 1 - i);
			Console.Write("*");
			Console.Write("\n");
		}
		//Display bottom half pattern

		for (int i = 0; i < size; i += 2) {
			Console.Write("*");
			space(i);
			Console.Write("*");
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args) {
		MyPattern obj = new MyPattern();
		obj.show_k(5);
		obj.show_k(7);
		obj.show_k(3);
		obj.show_k(9);
	}
}

Output

Size : 5

*    *
*  *
**
*  *
*    *

Size : 7

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

Size : 3

*  *
**
*  *

Size : 9

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

	public 	function space($size) {
		for ($i = 0; $i < $size; ++$i) {
			echo(" ");
		}
	}
	public 	function show_k($size) {
		if ($size < 2 ||
			$size % 2 == 0) {
			return;
		}
		echo("Size : ". $size ."\n\n");
		//Display top half pattern

		for ($i = 0; $i < $size - 2; $i += 2) {
			echo("*");
			$this->space($size - 1 - $i);
			echo("*");
			echo("\n");
		}
		//Display bottom half pattern

		for ($i = 0; $i < $size; $i += 2) {
			echo("*");
			$this->space($i);
			echo("*");
			echo("\n");
		}
		echo("\n");
	}
}

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

}
main();

Output

Size : 5

*    *
*  *
**
*  *
*    *

Size : 7

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

Size : 3

*  *
**
*  *

Size : 9

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

		process.stdout.write("Size : " + size + "\n\n");
		//Display top half pattern

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

		//Display bottom half pattern

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

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

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

main();

Output

Size : 5

*    *
*  *
**
*  *
*    *

Size : 7

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

Size : 3

*  *
**
*  *

Size : 9

*        *
*      *
*    *
*  *
**
*  *
*    *
*      *
*        *
#  Python 3 Program
#  Display k pattern 
class MyPattern :
	# include space 
	def space(self, size) :
		i = 0
		while (i < size) :
			print(" ", end = "")
			i += 1
		
	
	def show_k(self, size) :
		if (size < 2 or size % 2 == 0) :
			return
		
		print("Size : ", size ,"\n\n", end = "")
		# Display top half pattern
		i = 0
		while (i < size - 2) :
			print("*", end = "")
			self.space(size - 1 - i)
			print("*", end = "")
			print("\n", end = "")
			i += 2
		
		# Display bottom half pattern
		i = 0
		while (i < size) :
			print("*", end = "")
			self.space(i)
			print("*", end = "")
			print(end = "\n")
			i += 2
		
		print("\n", end = "")
	

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


if __name__ == "__main__":
	main()

Output

Size :  5

*    *
*  *
**
*  *
*    *

Size :  7

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

Size :  3

*  *
**
*  *

Size :  9

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

Output

Size  :5

*    *
*  *
**
*  *
*    *

Size  :7

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

Size  :3

*  *
**
*  *

Size  :9

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

// Scala Program
// Display k pattern 
class MyPattern {
	//include space 
	def space(size: Int): Unit = {
		var i: Int = 0;
		while (i < size) {
			print(" ");
			i += 1;
		}
	}
	def show_k(size: Int): Unit = {
		if (size < 2 ||
			size % 2 == 0) {
			return;
		}
		print("Size : " + size + "\n\n");

		//Display top half pattern
		var i: Int = 0;
		while (i < size - 2) {
			print("*");
			space(size - 1 - i);
			print("*");
			print("\n");
			i += 2;
		}
		//Display bottom half pattern
		i = 0;
		while (i < size) {
			print("*");
			space(i);
			print("*");
			print("\n");
			i += 2;
		}
		print("\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		obj.show_k(5);
		obj.show_k(7);
		obj.show_k(3);
		obj.show_k(9);
	}
}

Output

Size : 5

*    *
*  *
**
*  *
*    *

Size : 7

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

Size : 3

*  *
**
*  *

Size : 9

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

Output

Size :  5

*    *
*  *
**
*  *
*    *

Size :  7

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

Size :  3

*  *
**
*  *

Size :  9

*        *
*      *
*    *
*  *
**
*  *
*    *
*      *
*        *
// Rust Program
// Display k pattern 
fn main() {
	//Test Case
	show_k(5);
	show_k(7);
	show_k(3);
	show_k(9);
}
fn show_k(size: i32) {
	if size < 2 || size % 2 == 0 {
		return;
	}
	print!("Size : {}\n\n", size);
	let mut i: i32 = 0;
	//Display top half pattern

	while i < size - 2 {
		print!("*");
		space(size - 1 - i);
		print!("*");
		print!("\n");
		i += 2;
	}
	i = 0;
	//Display bottom half pattern

	while i < size {
		print!("*");
		space(i);
		print!("*");
		print!("\n");
		i += 2;
	}
	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

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


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