Pattern 242

Pattern 242 post thumbnail image

C

#include <stdio.h>

int main()
{
  int n = 7;
  int x = 1;
  int i,j;

  for(i = 1; i <= n; i++)
  {
    for(j = 1; j <= n; j++)
    {
      if (i % 2 == 1 && j <= x)
      {
        printf("* ");
      }
      else if (i % 2 == 0 && j >= n - x + 1)
      {
        printf("* ");
      }
      else
      {
        printf("# ");
      }
    }
    printf("\n");
    if (i <= n / 2)
      x++;
    else
      x--;
  }
  return 0;
}

C++

#include <iostream.h>

int main()
{
  int n = 7;
  int x = 1;
  
  
  for(int i = 1; i <= n; i++)
  {
    for(int j = 1; j <= n; j++)
    {
      if (i % 2 == 1 && j <= x)
      {
        cout<<"* ";
      }
      else if (i % 2 == 0 && j >= n - x + 1)
      {
        cout<<"* ";
      }
      else
      {
        cout<<"# ";
      }
    }
    cout<<endl;
    if (i <= n / 2)
      x++;
    else
      x--;
  }
  return 0;
}

Java

class PatternProg
{
	public static void main(String args[])
	{
	  int n = 7;
	  int x = 1;


	  for (int i = 1; i <= n; i++)
	  {
		for (int j = 1; j <= n; j++)
		{
		  if (i % 2 == 1 && j <= x)
		  {
			System.out.print("* ");
		  }
		  else if (i % 2 == 0 && j >= n - x + 1)
		  {
			System.out.print("* ");
		  }
		  else
		  {
			System.out.print("# ");
		  }
		}
		System.out.println();
		if (i <= n / 2)
		{
		  x++;
		}
		else
		{
		  x--;
		}
	  }
	  
	}
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 7;
    int x = 1;


    for (int i = 1; i <= n; i++)
    {
      for (int j = 1; j <= n; j++)
      {
        if (i % 2 == 1 && j <= x)
        {
          Console.Write("* ");
        }
        else if (i % 2 == 0 && j >= n - x + 1)
        {
          Console.Write("* ");
        }
        else
        {
          Console.Write("# ");
        }
      }
      Console.WriteLine();
      if (i <= n / 2)
      {
        x++;
      }
      else
      {
        x--;
      }
    }

    Console.ReadKey(true);
  }
}

Python

n = 7
d = 1

for x in range(1, n + 1):
  for y in range(1, n + 1):
    if x % 2 == 1 and y <= d:
       print("* ", end="")
    elif x % 2 == 0 and y >= n - d + 1:
       print("* ", end="")
    else:
       print("# ", end="")

  print()

  if x <= n // 2:
     d += 1
  else:
     d -= 1
5 1 vote
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Patterns