# Print W pattern

In this article, we will discuss how to print a W pattern using C programming language. We will explain the problem statement, provide a suitable example, describe the algorithm and pseudocode, and analyze the time complexity of the code. Let's get started!

## Problem Statement

The problem is to print a W pattern using asterisks (*) and spaces. The size of the pattern will be provided as input.

### Example

For better understanding, let's consider an example where the size is 5:

```    *
*
*   *
*   *   *
*
```

This is the desired output for the given size of 5. The pattern forms the shape of the letter "W".

## Algorithm and Pseudocode

To solve this problem, we can use nested loops to iterate over the rows and columns of the pattern. Here's the algorithm:

1. Check if the size is even. If it is, return because a W pattern cannot be formed with an even size.
2. Calculate the mid-point of the pattern as size/2.
3. Iterate over the rows from 0 to size-1.
4. For each row, iterate over the columns from 0 to size-1.
5. Inside the inner loop, check the following conditions:
• If the column is the first column, the last column, or one before the last column, print an asterisk (*) symbol.
• If the row is greater than or equal to the mid-point and is equal to the column, or if the row is greater than the mid-point and the column is the reflection of the row, print an asterisk (*) symbol.
• Otherwise, print two spaces to create a gap between the asterisks.
6. After each row is printed, move to the next line.

Here's the pseudocode for the above algorithm:

``````function print_space(size):
for counter from 0 to size-1:
print " "

function print_w(size):
if size is even:
return
mid = size / 2
for i from 0 to size-1:
for j from 0 to size-1:
if j is 0 or j+1 is size or j is size-1 or (i >= mid and i is j) or (i > mid and size-1-i is j):
print "*"
else:
print_space(2)
print newline

main:
print_w(5)
print_w(9)
print_w(13)
``````

## Code Solution

``````//C Program
//Print W pattern
#include <stdio.h>
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
printf(" ");
}
}
void print_w(int size)
{
if (size % 2 == 0)
{
return;
}
printf("\nSize : %d\n\n", size);
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0
|| j + 1 == size
|| j == size - 1
|| (i >= mid && i == j)
|| (i > mid && size - 1 - i == j))
{
printf(" *");
}
else
{
//include double space
print_space(2);
}
}
printf("\n");
}
}
int main()
{
//Simple test
print_w(5);
print_w(9);
print_w(13);
return 0;
}``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````/*
Java Program
Print W pattern
*/
class MyPattern
{
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
System.out.print(" ");
}
}
public void print_w(int size)
{
if (size % 2 == 0)
{
return;
}
System.out.print("\nSize : " + size + "\n\n");
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0 || j + 1 == size || j == size - 1 || (i >= mid && i == j) || (i > mid && size - 1 - i == j))
{
System.out.print(" *");
}
else
{
//include double space
print_space(2);
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_w(5);
obj.print_w(9);
obj.print_w(13);
}
}``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````/*
C++ Program
Print W pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
public:
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << " ";
}
}
void print_w(int size)
{
if (size % 2 == 0)
{
return;
}
cout << "\nSize : " << size << "\n\n";
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0 || j + 1 == size || j == size - 1 || (i >= mid && i == j) || (i > mid && size - 1 - i == j))
{
cout << " *";
}
else
{
//include double space
this->print_space(2);
}
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj =  MyPattern();
//Simple test
obj.print_w(5);
obj.print_w(9);
obj.print_w(13);
return 0;
}``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````/*
C# Program
Print W pattern
*/
using System;
class MyPattern
{
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
Console.Write(" ");
}
}
public void print_w(int size)
{
if (size % 2 == 0)
{
return;
}
Console.Write("\nSize : " + size + "\n\n");
int mid = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
//Test case which is printing the value of star
if (j == 0 || j + 1 == size || j == size - 1 || (i >= mid && i == j) || (i > mid && size - 1 - i == j))
{
Console.Write(" *");
}
else
{
//include double space
print_space(2);
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_w(5);
obj.print_w(9);
obj.print_w(13);
}
}``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````<?php
/*
Php Program
Print W pattern
*/
class MyPattern
{
//Display space of given size
public	function print_space(\$size)
{
\$counter = 0;
for (\$counter = 0; \$counter < \$size; \$counter++)
{
echo(" ");
}
}
public	function print_w(\$size)
{
if (\$size % 2 == 0)
{
return;
}
echo("\nSize : ". \$size ."\n\n");
\$mid = intval(\$size / 2);
for (\$i = 0; \$i < \$size; ++\$i)
{
for (\$j = 0; \$j < \$size; ++\$j)
{
//Test case which is printing the value of star
if (\$j == 0 || \$j + 1 == \$size || \$j == \$size - 1 || (\$i >= \$mid && \$i == \$j) || (\$i > \$mid && \$size - 1 - \$i == \$j))
{
echo(" *");
}
else
{
//include double space
\$this->print_space(2);
}
}
echo("\n");
}
}
}

function main()
{
\$obj = new MyPattern();
//Simple test
\$obj->print_w(5);
\$obj->print_w(9);
\$obj->print_w(13);
}
main();``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````/*
Node Js Program
Print W pattern
*/
class MyPattern
{
//Display space of given size
print_space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
process.stdout.write(" ");
}
}
print_w(size)
{
if (size % 2 == 0)
{
return;
}
process.stdout.write("\nSize : " + size + "\n\n");
var mid = parseInt(size / 2);
for (var i = 0; i < size; ++i)
{
for (var j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0 || j + 1 == size || j == size - 1 || (i >= mid && i == j) || (i > mid && size - 1 - i == j))
{
process.stdout.write(" *");
}
else
{
//include double space
this.print_space(2);
}
}
process.stdout.write("\n");
}
}
}

function main(args)
{
var obj = new MyPattern();
//Simple test
obj.print_w(5);
obj.print_w(9);
obj.print_w(13);
}
main();``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````#   Python 3 Program
#   Print W pattern

class MyPattern :
# Display space of given size
def print_space(self, size) :
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1

def print_w(self, size) :
if (size % 2 == 0) :
return

print("\nSize : ", size ,"\n\n", end = "")
mid = int(size / 2)
i = 0
j = 0
while (i < size) :
j = 0
while (j < size) :
# Test case which is printing the value of star
if (j == 0 or j + 1 == size or j == size - 1 or(i >= mid and i == j) or(i > mid and size - 1 - i == j)) :
print(" *", end = "")
else :
# include double space
self.print_space(2)

j += 1

print("\n", end = "")
i += 1

def main() :
obj = MyPattern()
# Simple test
obj.print_w(5)
obj.print_w(9)
obj.print_w(13)

if __name__ == "__main__": main()``````

#### Output

``````Size :  5

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

Size :  9

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

Size :  13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````# Ruby Program
# Print W pattern

class MyPattern

# Display space of given size
def print_space(size)

counter = 0
while (counter < size)

print(" ")
counter += 1
end
end
def print_w(size)

if (size % 2 == 0)

return
end
print("\nSize : ", size ,"\n\n")
mid = size / 2
i = 0
j = 0
while (i < size)

j = 0
while (j < size)

# Test case which is printing the value of star
if (j == 0 || j + 1 == size || j == size - 1 || (i >= mid && i == j) || (i > mid && size - 1 - i == j))

print(" *")
else

# include double space
self.print_space(2)
end
j += 1
end
print("\n")
i += 1
end
end
end
def main()

obj = MyPattern.new()
# Simple test
obj.print_w(5)
obj.print_w(9)
obj.print_w(13)
end
main()``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *
``````
``````/*
Scala Program
Print W pattern
*/
class MyPattern
{
//Display space of given size
def print_space(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
print(" ");
counter += 1;
}
}
def print_w(size: Int): Unit = {
if (size % 2 == 0)
{
return;
}
print("\nSize : " + size + "\n\n");
var mid: Int = (size / 2).toInt;
var i: Int = 0;
var j: Int = 0;
while (i < size)
{
j = 0;
while (j < size)
{
//Test case which is printing the value of star
if (j == 0 || j + 1 == size || j == size - 1 || (i >= mid && i == j) || (i > mid && size - 1 - i == j))
{
print(" *");
}
else
{
//include double space
print_space(2);
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_w(5);
obj.print_w(9);
obj.print_w(13);
}
}``````

#### Output

``````Size : 5

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

Size : 9

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

Size : 13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````
``````/*
Swift Program
Print W pattern
*/
class MyPattern
{
//Display space of given size
func print_space(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
print(" ", terminator: "");
counter += 1;
}
}
func print_w(_ size: Int)
{
if (size % 2 == 0)
{
return;
}
print("\nSize : ", size ,"\n\n", terminator: "");
let mid: Int = size / 2;
var i: Int = 0;
var j: Int = 0;
while (i < size)
{
j = 0;
while (j < size)
{
//Test case which is printing the value of star
if (j == 0 || j + 1 == size || j == size - 1 || (i >= mid && i == j) || (i > mid && size - 1 - i == j))
{
print(" *", terminator: "");
}
else
{
//include double space
self.print_space(2);
}
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_w(5);
obj.print_w(9);
obj.print_w(13);
}
main();``````

#### Output

``````Size :  5

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

Size :  9

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

Size :  13

*                       *
*                       *
*                       *
*                       *
*                       *
*                       *
*           *           *
*         *   *         *
*       *       *       *
*     *           *     *
*   *               *   *
* *                   * *
*                       *``````

## Time Complexity

The time complexity of the given code is O(n^2), where n represents the size of the pattern. This is because the code uses nested loops to iterate over the rows and columns, resulting in a quadratic time complexity.

That's it! You now have a complete understanding of how to print a W pattern using C programming. Feel free to modify the code and experiment with different sizes to create various patterns.

## 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.