Display Kite Pattern
In this article, we will discuss how to display a kite pattern using a C program. The kite pattern is formed by a combination of symbols and spaces, creating a visual representation of a kite. We will provide an explanation of the problem, present the code solution, discuss the algorithm and pseudocode, and explain the output with the time complexity of the code.
Introduction
The problem at hand is to create a program that displays a kite pattern using ASCII symbols. The pattern consists of a diamond shape with a vertical line passing through the center, resembling a kite. The size of the kite pattern is determined by the user input.
Problem Statement
The task is to write a C program that takes an input for the size of the kite pattern and displays it on the console. The program should print the kite pattern using ASCII symbols, including spaces, forward slashes, backslashes, vertical bars, hyphens, and tildes.
For example, if the size is given as 3, the program should output:
^ /|\ /-|-\ /|\ /-\ ~~~
This represents a kite pattern with size 3.
Algorithm and Pseudocode
1. Start the program.
2. Define the functions: space()
, print_pattern()
, and kite_pattern()
.
3. Implement the space()
function to print spaces based on the given size.
4. Implement the print_pattern()
function to print the pattern based on the given size.
5. Implement the kite_pattern()
function to display the entire kite pattern.
6. Inside the kite_pattern()
function, display the upper half portion of the kite pattern using a loop:
- Print spaces using the space()
function.
- Print the pattern using the print_pattern()
function.
7. After displaying the upper half portion, display the bottom half portion of the kite pattern using another loop:
- If it is the last iteration, print the bottom of the kite pattern with specific symbols.
- Otherwise, print spaces using the space()
function.
- Print the pattern using the print_pattern()
function.
8. End the program.
Pseudocode
function space(size):
for counter from 0 to size:
print a space
function print_pattern(size):
for counter from 0 to size:
if counter is 0:
print a forward slash
else if counter is size - 1:
print a backslash
else:
if counter is size/2 - 1:
print a vertical bar
increment counter
else:
print a hyphen
function kite_pattern(size):
print "Size: size"
for i from 1 to size:
call space(size - i)
if i is 1:
print a caret symbol and move to the next iteration
call print_pattern(i * 2)
for i from 0 to size:
if i is size - 1:
call space(i - 1)
print "/-\\"
call space(i - 1)
print "~~~"
else:
call space(i)
call print_pattern((size - i) * 2)
main:
call kite_pattern(3)
call kite_pattern(5)
call kite_pattern(7)
Code Solution
//C Program
//Display Kite Pattern
#include <stdio.h>
#include <stdlib.h>
/*include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
printf(" ");
}
}
/*Print value of given size*/
void print_pattern(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0) {
/*Print symbol*/
printf("/");
} else if (counter == size - 1) {
/*Print symbol*/
printf("\\");
} else {
/*Add intermediate values*/
if(counter==size/2 -1)
{
printf("|");
counter++;
}
else
{
printf("-");
}
}
}
}
//Display kite pattern
void kite_pattern(int size) {
printf("\n Size : %d \n\n", size);
int i = 0, j = 0;
//Display upper half portion
for (i = 1; i < size; i++) {
space(size - i );
if(i==1)
{
printf("^\n");
continue;
}
print_pattern(i *2);
printf("\n");
}
//Display bottom half portion
for (i = 0; i < size; i++) {
if(i==size-1)
{
//Display bottom of kite
space(i-1);
printf("/-\\\n");
space(i-1);
printf("~~~");
}
else
{
space(i);
print_pattern((size - i) *2);
}
printf("\n");
}
}
int main() {
//Test Cases
kite_pattern(3);
kite_pattern(5);
kite_pattern(7);
return 0;
}
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
/*
C++ Program
Display Kite Pattern
*/
#include<iostream>
using namespace std;
class MyPattern {
public:
/*include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
cout << " ";
}
}
/*Print value of given size*/
void print_pattern(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0) {
/*Print symbol*/
cout << "/";
} else
if (counter == size - 1) {
/*Print symbol*/
cout << "\\";
} else {
/*Add intermediate values*/
if (counter == size / 2 - 1) {
cout << "|";
counter++;
} else {
cout << "-";
}
}
}
}
//Display kite pattern
void kite_pattern(int size) {
if (size < 3) {
return;
}
cout << "\n Size : " << size << " \n\n";
int i = 0, j = 0;
//Display upper half portion
for (i = 1; i < size; i++) {
this->space(size - i);
if (i == 1) {
cout << "^";
} else {
this->print_pattern(i *2);
}
cout << "\n";
}
//Display bottom half portion
for (i = 0; i < size; i++) {
if (i == size - 1) {
//Display bottom of kite
this->space(i - 1);
cout << "/-\\\n";
this->space(i - 1);
cout << "~~~";
} else {
this->space(i);
this->print_pattern((size - i) *2);
}
cout << "\n";
}
}
};
int main() {
MyPattern obj = MyPattern();
//Test Cases
obj.kite_pattern(3);
obj.kite_pattern(5);
obj.kite_pattern(7);
return 0;
}
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
/*
Java Program
Display Kite Pattern
*/
public class MyPattern {
/*include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
System.out.print(" ");
}
}
/*Print value of given size*/
public void print_pattern(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0) {
/*Print symbol*/
System.out.print("/");
} else if (counter == size - 1) {
/*Print symbol*/
System.out.print("\\");
} else {
/*Add intermediate values*/
if(counter==size/2 -1)
{
System.out.print("|");
counter++;
}
else
{
System.out.print("-");
}
}
}
}
//Display kite pattern
public void kite_pattern(int size) {
if(size<3)
{
return;
}
System.out.print("\n Size : "+size+" \n\n");
int i = 0;
//Display upper half portion
for (i = 1; i < size; i++) {
space(size - i );
if(i==1)
{
System.out.print("^");
}
else
{
print_pattern(i *2);
}
System.out.print("\n");
}
//Display bottom half portion
for (i = 0; i < size; i++) {
if(i==size-1)
{
//Display bottom of kite
space(i-1);
System.out.print("/-\\\n");
space(i-1);
System.out.print("~~~");
}
else
{
space(i);
print_pattern((size - i) *2);
}
System.out.print("\n");
}
}
public static void main(String[] args) {
MyPattern obj = new MyPattern();
//Test Cases
obj.kite_pattern(3);
obj.kite_pattern(5);
obj.kite_pattern(7);
}
}
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
/*
C# Program
Display Kite Pattern
*/
using System;
public class MyPattern {
/*include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
Console.Write(" ");
}
}
/*Print value of given size*/
public void print_pattern(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0) {
Console.Write("/");
} else
if (counter == size - 1) {
Console.Write("\\");
} else {
/*Add intermediate values*/
if (counter == size / 2 - 1) {
Console.Write("|");
counter++;
} else {
Console.Write("-");
}
}
}
}
//Display kite pattern
public void kite_pattern(int size) {
if (size < 3) {
return;
}
Console.Write("\n Size : " + size + " \n\n");
int i = 0;
//Display upper half portion
for (i = 1; i < size; i++) {
space(size - i);
if (i == 1) {
Console.Write("^");
} else {
print_pattern(i * 2);
}
Console.Write("\n");
}
//Display bottom half portion
for (i = 0; i < size; i++) {
if (i == size - 1) {
space(i - 1);
Console.Write("/-\\\n");
space(i - 1);
Console.Write("~~~");
} else {
space(i);
print_pattern((size - i) * 2);
}
Console.Write("\n");
}
}
public static void Main(String[] args) {
MyPattern obj = new MyPattern();
obj.kite_pattern(3);
obj.kite_pattern(5);
obj.kite_pattern(7);
}
}
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
<?php
/*
Php Program
Display Kite Pattern
*/
class MyPattern {
/*include Space of given size*/
public function space($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
//Add space
echo(" ");
}
}
/*Print value of given size*/
public function print_pattern($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
if ($counter == 0) {
/*Print symbol*/
echo("/");
} else
if ($counter == $size - 1) {
/*Print symbol*/
echo("\\");
} else {
/*Add intermediate values*/
if ($counter == intval($size / 2) - 1) {
echo("|");
$counter++;
} else {
echo("-");
}
}
}
}
//Display kite pattern
public function kite_pattern($size) {
if ($size < 3) {
return;
}
echo("\n Size : ". $size ." \n\n");
$i = 0;
//Display upper half portion
for ($i = 1; $i < $size; $i++) {
$this->space($size - $i);
if ($i == 1) {
echo("^");
} else {
$this->print_pattern($i *2);
}
echo("\n");
}
//Display bottom half portion
for ($i = 0; $i < $size; $i++) {
if ($i == $size - 1) {
//Display bottom of kite
$this->space($i - 1);
echo("/-\\\n");
$this->space($i - 1);
echo("~~~");
} else {
$this->space($i);
$this->print_pattern(($size - $i) *2);
}
echo("\n");
}
}
}
function main() {
$obj = new MyPattern();
//Test Cases
$obj->kite_pattern(3);
$obj->kite_pattern(5);
$obj->kite_pattern(7);
}
main();
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
/*
Node Js Program
Display Kite Pattern
*/
class MyPattern {
/*include Space of given size*/
space(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
process.stdout.write(" ");
}
}
/*Print value of given size*/
print_pattern(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0) {
/*Print symbol*/
process.stdout.write("/");
} else
if (counter == size - 1) {
/*Print symbol*/
process.stdout.write("\\");
} else {
/*Add intermediate values*/
if (counter == parseInt(size / 2) - 1) {
process.stdout.write("|");
counter++;
} else {
process.stdout.write("-");
}
}
}
}
//Display kite pattern
kite_pattern(size) {
if (size < 3) {
return;
}
process.stdout.write("\n Size : " + size + " \n\n");
var i = 0;
//Display upper half portion
for (i = 1; i < size; i++) {
this.space(size - i);
if (i == 1) {
process.stdout.write("^");
} else {
this.print_pattern(i *2);
}
process.stdout.write("\n");
}
//Display bottom half portion
for (i = 0; i < size; i++) {
if (i == size - 1) {
//Display bottom of kite
this.space(i - 1);
process.stdout.write("/-\\\n");
this.space(i - 1);
process.stdout.write("~~~");
} else {
this.space(i);
this.print_pattern((size - i) *2);
}
process.stdout.write("\n");
}
}
}
function main(args) {
var obj = new MyPattern();
//Test Cases
obj.kite_pattern(3);
obj.kite_pattern(5);
obj.kite_pattern(7);
}
main();
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
# Python 3 Program
# Display Kite Pattern
class MyPattern :
# include Space of given size
def space(self, size) :
counter = 0
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
# Print value of given size
def print_pattern(self, size) :
counter = 0
counter = 0
while (counter < size) :
if (counter == 0) :
print("/", end = "")
elif (counter == size - 1) :
print("\\", end = "")
else :
# Add intermediate values
if (counter == int(size / 2) - 1) :
print("|", end = "")
counter += 1
else :
print("-", end = "")
counter += 1
# Display kite pattern
def kite_pattern(self, size) :
if (size < 3) :
return
print("\n Size : ", size ," \n\n", end = "")
i = 0
# Display upper half portion
i = 1
while (i < size) :
self.space(size - i)
if (i == 1) :
print("^", end = "")
else :
self.print_pattern(i * 2)
print("\n", end = "")
i += 1
# Display bottom half portion
i = 0
while (i < size) :
if (i == size - 1) :
self.space(i - 1)
print("/-\\\n", end = "")
self.space(i - 1)
print("~~~", end = "")
else :
self.space(i)
self.print_pattern((size - i) * 2)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
obj.kite_pattern(3)
obj.kite_pattern(5)
obj.kite_pattern(7)
if __name__ == "__main__":
main()
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
# Ruby Program
# Display Kite Pattern
class MyPattern
# include Space of given size
def space(size)
counter = 0
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
# Print value of given size
def print_pattern(size)
counter = 0
counter = 0
while (counter < size)
if (counter == 0)
print("/")
elsif (counter == size - 1)
print("\\")
else
# Add intermediate values
if (counter == size / 2 - 1)
print("|")
counter += 1
else
print("-")
end
end
counter += 1
end
end
# Display kite pattern
def kite_pattern(size)
if (size < 3)
return
end
print("\n Size :", size ," \n\n")
i = 0
# Display upper half portion
i = 1
while (i < size)
self.space(size - i)
if (i == 1)
print("^")
else
self.print_pattern(i * 2)
end
print("\n")
i += 1
end
# Display bottom half portion
i = 0
while (i < size)
if (i == size - 1)
self.space(i - 1)
print("/-\\\n")
self.space(i - 1)
print("~~~")
else
self.space(i)
self.print_pattern((size - i) * 2)
end
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
obj.kite_pattern(3)
obj.kite_pattern(5)
obj.kite_pattern(7)
end
main()
Output
Size :3
^
/|\
/-|-\
/|\
/-\
~~~
Size :5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size :7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
/*
Scala Program
Display Kite Pattern
*/
class MyPattern {
/*include Space of given size*/
def space(size: Int): Unit = {
var counter: Int = 0;
counter = 0;
while (counter < size) {
print(" ");
counter += 1;
}
}
/*Print value of given size*/
def print_pattern(size: Int): Unit = {
var counter: Int = 0;
counter = 0;
while (counter < size) {
if (counter == 0) {
print("/");
} else
if (counter == size - 1) {
print("\\");
} else {
/*Add intermediate values*/
if (counter == (size / 2).toInt - 1) {
print("|");
counter += 1;
} else {
print("-");
}
}
counter += 1;
}
}
//Display kite pattern
def kite_pattern(size: Int): Unit = {
if (size < 3) {
return;
}
print("\n Size : " + size + " \n\n");
var i: Int = 0;
//Display upper half portion
i = 1;
while (i < size) {
this.space(size - i);
if (i == 1) {
print("^");
} else {
this.print_pattern(i * 2);
}
print("\n");
i += 1;
}
//Display bottom half portion
i = 0;
while (i < size) {
if (i == size - 1) {
this.space(i - 1);
print("/-\\\n");
this.space(i - 1);
print("~~~");
} else {
this.space(i);
this.print_pattern((size - i) * 2);
}
print("\n");
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyPattern = new MyPattern();
obj.kite_pattern(3);
obj.kite_pattern(5);
obj.kite_pattern(7);
}
}
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
/*
Swift Program
Display Kite Pattern
*/
class MyPattern {
/*include Space of given size*/
func space(_ size: Int) {
var counter = 0;
counter = 0;
while (counter < size) {
print(" ", terminator: "");
counter += 1;
}
}
/*Print value of given size*/
func print_pattern(_ size: Int) {
var counter = 0;
counter = 0;
while (counter < size) {
if (counter == 0) {
print("/", terminator: "");
} else
if (counter == size - 1) {
print("\\", terminator: "");
} else {
/*Add intermediate values*/
if (counter == size / 2 - 1) {
print("|", terminator: "");
counter += 1;
} else {
print("-", terminator: "");
}
}
counter += 1;
}
}
//Display kite pattern
func kite_pattern(_ size: Int) {
if (size < 3) {
return;
}
print("\n Size : ", size ," \n\n", terminator: "");
var i = 0;
//Display upper half portion
i = 1;
while (i < size) {
self.space(size - i);
if (i == 1) {
print("^", terminator: "");
} else {
self.print_pattern(i * 2);
}
print("\n", terminator: "");
i += 1;
}
//Display bottom half portion
i = 0;
while (i < size) {
if (i == size - 1) {
self.space(i - 1);
print("/-\\\n", terminator: "");
self.space(i - 1);
print("~~~", terminator: "");
} else {
self.space(i);
self.print_pattern((size - i) * 2);
}
print("\n", terminator: "");
i += 1;
}
}
}
func main() {
let obj = MyPattern();
obj.kite_pattern(3);
obj.kite_pattern(5);
obj.kite_pattern(7);
}
main();
Output
Size : 3
^
/|\
/-|-\
/|\
/-\
~~~
Size : 5
^
/|\
/-|-\
/--|--\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Size : 7
^
/|\
/-|-\
/--|--\
/---|---\
/----|----\
/-----|-----\
/----|----\
/---|---\
/--|--\
/-|-\
/|\
/-\
~~~
Explanation and Output
Let's walk through the code and understand how the kite pattern is displayed for different sizes. We will also discuss the time complexity of the code.
The main function calls the kite_pattern()
function with different sizes as test cases. The kite_pattern()
function starts by printing the size of the pattern.
Next, we enter a loop to display the upper half portion of the kite pattern. We use the space()
function to print the appropriate number of spaces before each line. In the first iteration of the loop, when i
is 1, we print a caret symbol ('^') to represent the top of the kite. For subsequent iterations, we call the print_pattern()
function to display the pattern.
After displaying the upper half, we enter another loop to display the bottom half portion of the kite pattern. In the last iteration of this loop, we print the bottom of the kite with specific symbols: "/-\\", which represents the bottom corners, and "~~~", which represents the tail of the kite. For other iterations, we call the space()
and print_pattern()
functions to display the pattern.
The output is displayed for each test case, showing the kite pattern for different sizes as requested.
The time complexity of this code is O(n^2), where n is the size of the pattern. This is because there are two nested loops, one for the upper half and one for the bottom half, each running up to the given size. The space and pattern functions have linear time complexity, but since they are called within loops, the overall time complexity becomes quadratic.
Finally
In this article, we have explained the problem of displaying a kite pattern using a C program. We provided a step-by-step explanation of the code, including the algorithm, pseudocode, and output. The code uses loops and helper functions to print the desired pattern based on the given size. By following the provided instructions and understanding the code, you can generate kite patterns of different sizes. This article serves as a guide to implement a program that displays the kite pattern using ASCII symbols in a visually appealing manner.
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