Pattern 437

Pattern 437 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++);
        }
      }
      else
      {
        printf("   "); // 3ws
      }
    }
    printf("\n");
    total = total - (n - i + 1);
  }
  return 0;
}

C++

#include <iostream.h>
#include <iomanip.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<<setw(3)<<px++;
        }
        else
        {
          cout<<setw(3)<<py++;
        }
      }
      else
      {
        cout<<setw(3)<<" ";
      }
    }
    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("%3d", px++);
			}
			else
			{
			  System.out.printf("%3d", py++);
			}
		  }
		  else
		  {
			System.out.print("   "); //3ws
		  }
		}
		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,3:D}", px++);
          }
          else
          {
            Console.Write("{0,3:D}", py++);
          }
        }
        else
        {
          Console.Write("   "); //3ws
        }
      }
      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("{:3d}".format(px), end="")
          px += 1
       else:
          print("{:3d}".format(py), end="")
          py += 1
    else:
       print("   ", end="")  # 3ws
  print()
  total = total - (n - x + 1)
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Patterns