Pattern 259

Pattern 259 post thumbnail image

C

#include <stdio.h>

int main()
{
  int n = 4;
  int total = (n * (n + 1) / 2);
  int px = 1,py;
  int i,j;

  for(i = 1; i <= n; i++)
  {
    py = total - (n - i);
    for (j = 1; j <= n * 2; j++)
    {
      if (j > 2 * (i - 1))
      {
        if (j <= (n * 2) / 2 + i - 1)
        {
          printf("%2d ", px++);
        }
        else
        {
          printf("%2d ", py++);
        }
      }
    }
    printf("\n");
    total = total - (n - i + 1);
  }
  return 0;
}

C++

#include <iostream.h>

int main()
{
  int n = 4;
  int total = (n * (n + 1) / 2);
  int px = 1,py;
  

  for(int i = 1; i <= n; i++)
  {
    py = total - (n - i);
    for(int j = 1; j <= n * 2; j++)
    {
      if (j > 2 * (i - 1))
      {
        if (j <= (n * 2) / 2 + i - 1)
        {
          cout<<px++<<" ";
        }
        else
        {
          cout<<py++<<" ";
        }
      }
    }
    cout<<endl;
    total = total - (n - i + 1);
  }
  return 0;
}

Java

class PatternProg
{
	public static void main(String args[])
	{
	  int n = 4;
	  int total = (n * (n + 1) / 2);
	  int px = 1;
	  int py;


	  for (int i = 1; i <= n; i++)
	  {
		py = total - (n - i);
		for (int j = 1; j <= n * 2; j++)
		{
		  if (j > 2 * (i - 1))
		  {
			if (j <= (n * 2) / 2 + i - 1)
			{
			  System.out.printf("%2d ", px++);
			}
			else
			{
			  System.out.printf("%2d ", py++);
			}
		  }
		}
		System.out.println();
		total = total - (n - i + 1);
	  }
	  
	}
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 4;
    int total = (n * (n + 1) / 2);
    int px = 1;
    int py;


    for (int i = 1; i <= n; i++)
    {
      py = total - (n - i);
      for (int j = 1; j <= n * 2; j++)
      {
        if (j > 2 * (i - 1))
        {
          if (j <= (n * 2) / 2 + i - 1)
          {
            Console.Write("{0,2:D} ", px++);
          }
          else
          {
            Console.Write("{0,2:D} ", py++);
          }
        }
      }
      Console.WriteLine();
      total = total - (n - i + 1);
    }

    Console.ReadKey(true);
  }
}

Python

n = 4
total = (n * (n + 1) // 2)
px = 1

for x in range(1, n + 1):
  py = total - (n - x)
  for y in range(1, n * 2 + 1):
      if y > 2 * (x - 1):
         if y <= (n * 2) // 2 + x - 1:
            print("{:2d} ".format(px), end="")
            px += 1
         else:
            print("{:2d} ".format(py), end="")
            py += 1
  print()
  total = total - (n - x + 1)
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns