Posted on by Kalkicode
Code Pattern

Print K pattern

In this article, we will discuss a program to display the K pattern using asterisks. The K pattern is formed by printing asterisks in a specific pattern that resembles the letter 'K'. We will provide an explanation of the problem statement, illustrate it with a suitable example, and present the algorithm and pseudocode for the solution. Finally, we will analyze the output with an explanation of each statement and discuss the time complexity of the code.

Problem Statement

The problem is to create a program that takes an integer size as input and prints a K pattern using asterisks. The size determines the height of the pattern, which should be an odd number greater than or equal to 3. The K pattern consists of two parts: the top half and the bottom half. The top half resembles the upper part of the letter 'K', and the bottom half resembles the lower part.

Example

Let's take an example to better understand the problem. Suppose the input size is 5. The program should generate the following pattern:

*    *
*  *
**
*  *
*    *

The pattern starts with an asterisk on the first row, followed by spaces and another asterisk. On the second row, there is an asterisk, more spaces, and another asterisk. The middle row contains only two asterisks. The bottom half is the mirror image of the top half.

Algorithm

The algorithm for generating the K pattern is as follows:

  1. Define a function called "space" that takes an integer size as input.
  2. Inside the "space" function, use a loop to print spaces "size" number of times.
  3. Define another function called "show_k" that takes an integer size as input.
  4. Inside the "show_k" function, check if the size is less than 2 or if it is an even number. If either condition is true, return from the function.
  5. Print the size of the pattern for debugging purposes.
  6. Use a loop to display the top half of the pattern. Start from 0 and iterate until "size-2" with a step size of 2.
  7. Inside the loop, print an asterisk, call the "space" function with the appropriate size, and print another asterisk.
  8. Print a newline character to move to the next row.
  9. Use another loop to display the bottom half of the pattern. Start from 0 and iterate until "size" with a step size of 2.
  10. Inside the second loop, print an asterisk, call the "space" function with the appropriate size, and print another asterisk.
  11. Print a newline character to move to the next row.
  12. Print an additional newline character for better readability.
  13. In the main function, call the "show_k" function with different test cases to display the patterns.

Pseudocode

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

function show_k(size):
    if size < 2 or size is even:
        return
    print "Size: ", size
    for i = 0 to size-2 with step 2:
        print "*"
        space(size-1-i)
        print "*"
        print newline
    for i = 0 to size with step 2:
        print "*"
        space(i)
        print "*"
        print newline
    print newline

main:
    show_k(5)
    show_k(7)
    show_k(3)
    show_k(9)

Code Solution

// 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

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

Output Explanation

The output for the given test cases is as follows:

Size: 5

*    *
*  *
**
*  *
*    *

Size: 7

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

Size: 3

*  *
**
*  *

Size: 9

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

The output displays the patterns for each test case. The size of the pattern is printed at the beginning for debugging purposes. Each asterisk is surrounded by spaces to create the desired pattern. The top half and the bottom half are displayed separately.

Time Complexity

The time complexity of this code is O(n), where n is the input size. Both loops iterate through the range of values based on the input size. The space function has a time complexity of O(n) as well since it performs a constant operation (printing a space) "size" number of times. Therefore, the overall time complexity is linear with respect to 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